我正在使用 react-bootstrap 的 ModalTrigger 来显示一个重字段模式(基于 react-bootstrap 的 Modal),这意味着向它发送一堆道具:
<ModalTrigger modal={<MyModal field1={value1} field2={value2} (more fields...)/>}>
Click here to open
</ModalTrigger>
创建触发器的父组件具有通过 props 传入的字段/值,并且该组件的父组件也通过实际保存数据的顶级组件将其作为 props 传递。两者基本上都是管道,这是一个经典的 childContext 场景,只是它不起作用。这是我尝试过的简化版本:
var MyModal = React.createClass({
contextTypes : {foo : React.PropTypes.string},
render : function() {
return (
<Modal {...this.props} title="MyTitle">
<div className="modal-body">
The context is {this.context.foo}
</div>
</Modal>
);
}
});
var Content = React.createClass({
childContextTypes : {foo: React.PropTypes.string},
getChildContext : function() {return {foo : "bar"}},
render : function() {
return (
<ModalTrigger modal={<MyModal/>}>
<span>Show modal</span>
</ModalTrigger>
)
}
});
模式弹出“上下文是”,但不显示实际上下文。
我相信这是因为发送到 ModalTrigger 的道具已经以某种方式渲染/安装,但我不知道为什么。据我了解,MyModal 的所有者是 Content 组件,这意味着上下文应该没问题,但事实并非如此。
更多信息:我已经尝试过传递{...this.props}
并传递context={this.context}
给 MyModal,但没有成功。此外,可能相关的是,ModalTrigger 使用 cloneElement 来确保模式的 onRequestHide 道具指向触发器的隐藏功能。
那么我在这里错过了什么?:/