在作业务的时候,有些模块是能够拖动的,刚好这些模块须要从根组件App的context上拿属性,同时App也是做为拖动上下文,被@DragDropContext(HTML5Backend)装饰,当时年少无知,无脑写下了如下代码javascript
const boxSource = { canDrag(props, monitor) { ... }, beginDrag(props) { ... }, endDrag(props, monitor) { ... }, }; @DragSource('box', boxSource, (connect, monitor) => ({ connectDragSource: connect.dragSource(), isDragging: monitor.isDragging(), })) export default class Box extends Component { static contextTypes = { value: PropTypes.number }; static propTypes = { ... } render() { const { isDragging, connectDragSource, src } = this.props; const { value } = this.context; return ( connectDragSource( ... ) ); } }
美滋滋啊,美滋滋啊,so ez,会用react-dnd了,赶忙将代码跑起来,结果傻眼了,竟然报这个错误html
Invariant Violation: Could not find the drag and drop manager in the context of Box. Make sure to wrap the top-level component of your app with DragDropContext. Read more: http://react-dnd.github.io/react-dnd/docs-troubleshooting.html#could-not-find-the-drag-and-drop-manager-in-the-context
提示咱们在拖拽组件Box的context上找不到react-dnd须要的drag and drop manager,懵了,让我想一想是咋回事,是否是最后给java
static contextTypes = { value: PropTypes.number }
给覆盖了原来的Box.contextTypes
呀?
不过这也简单,不让他覆盖就行了嘛,因而我写下了以下的代码react
Box.contextTypes = Object.assign(Box.contextTypes,{ value: PropTypes.number });
真好,报错消失了,大功告成!等等,this.context.value怎么是undefined
,拿不到了?我明明在contextTypes里声明了呀,不行,仍是得去看一看源码。git
查看DragSource的源码,能够看到DragSource就是一个普通装饰器包装函数github
function DragSource(type, spec, collect, options = {}) { ... return function decorateSource(DecoratedComponent) { return decorateHandler({ connectBackend: (backend, sourceId) => backend.connectDragSource(sourceId), containerDisplayName: 'DragSource', createHandler: createSource, registerHandler: registerSource, createMonitor: createSourceMonitor, createConnector: createSourceConnector, DecoratedComponent, getType, collect, options, }); }; }
那咱们继续去看一看 decorateHandler
这个函数呗app
export default function decorateHandler({ DecoratedComponent, createHandler, createMonitor, createConnector, registerHandler, containerDisplayName, getType, collect, options, }) { ... class DragDropContainer extends Component { ... static contextTypes = { dragDropManager: PropTypes.object.isRequired, } ... render() { return ( <DecoratedComponent {...this.props} {...this.state} ref={this.handleChildRef} /> ); } } return hoistStatics(DragDropContainer, DecoratedComponent); }
嗯, decorateHandler
就是一个HOC生成函数嘛,hoistStatics
就是hoist-non-react-statics
这个库,作过HOC的童鞋必定不陌生,他就是将WrappedComponent的静态方法和静态属性提到HOC上,面,避免WrappedComponent的静态属性和静态方法丢失了,看似挺合理,嗯嗯。等等!这不就用WrappedComponent的contextTypes将HOC的contextTypes给覆盖了么?这也很合理的解释了为啥会报错了。函数
知道了其中的原来,那咱们就让HOC和WrappedComponent各自保留一份contextTypes好了,首先咱们须要用另外一个变量来保留对WrappedComponent的引用,由于被@DragSource
装饰后,WrappedComponent的变量名就会被HOC覆盖了,而后咱们再对WrappedComponent加上contextTypes就行了,代码以下:ui
class Box extends Component { static propTypes = { connectDragSource: PropTypes.func.isRequired, ... } render() { const { isDragging, connectDragSource, src } = this.props; const { value } = this.context; ... return ( connectDragSource( ... ) ); } } const Temp = Box; const Box1 = DragSource('box', boxSource, (connect, monitor) => ({ connectDragSource: connect.dragSource(), isDragging: monitor.isDragging(), }))(Box); Temp.contextTypes = { value: PropTypes.number, } export default Box1;
大功告成,咱们再来跑一跑。
哇,又报错了,囧,说this
Invariant Violation: App.getChildContext(): childContextTypes must be defined in order to use getChildContext().
好,那咱们来看看根组件咋回事,我写的根组件以下
@DragDropContext(HTML5Backend) class App extends React.Component { constructor(props) { super(props); } static childContextTypes = { value:PropTypes.number, } getChildContext(){ return { value:1 } } render() { return ( <Box /> ) } }
让咱们看看DragDropContext
源码
export default function DragDropContext(backendOrModule) { ... return function decorateContext(DecoratedComponent) { ... class DragDropContextContainer extends Component { getChildContext() { return childContext; } render() { return ( <DecoratedComponent {...this.props} ref={(child) => { this.child = child; }} /> ); } } return hoistStatics(DragDropContextContainer, DecoratedComponent); }; }
得,又是HOC的问题,可是有点不一样,就是contextTypes必定要准确设置在须要的组件上,可是childContextTypes只要放在上层组件就能够了,因此我作了以下修改:
class App
中的static childContextType = { value: PropTypes.number }
App.childContextTypes = Object.assign(App.childContextTypes,{ value: PropTypes.number });
此次总该行了吧,心累啊。嗯?仍是拿不到this.context.value
,想起来了!,虽然hoist-non-react-statics
将静态属性拿了出来,可是原型方法不会拿出来啊,因此WrappedComponent的getChildContext
就没用了,因此咱们须要也将他拿出来,因而,加上一下代码
const temp = {...App.prototype.getChildContext()}; App.prototype.getChildContext = () => ({...temp, value:1})
此次总算拿到正确的结果了,开心