【发布时间】:2016-08-21 17:26:34
【问题描述】:
我有 3 个组件的情况; <View><Content>和<Footer>。
<View> 是 <Content> 和 <Footer> 的父级。现在<Footer> 中有一个按钮,当它被点击时,我想在<Content> 组件中渲染一个Overlay,所以我必须能够访问我的@987654332 中<Content> 组件的this @ 零件。我不知道如何做到这一点。
View.js
class View extends Component {
render() {
return (
<div>
<Content messages={this.props.messages}/>
<Footer />
</div>
)
}
}
Content.js
class Content extends Component {
render() {
return (
<div>
{this.props.messages}
</div>
)
}
}
Footer.js
class Footer extends Component {
constructor(props,context){
super(props,context);
this.state = {
showEmojiPicker: false,
}
}
handleToggleEmojiPicker() {
this.setState({
showEmojiPicker: !this.state.showEmojiPicker,
});
}
render() {
return (
<div>
<div>
<div>
<bootstrap.Button ref="targetEmojiPicker" bsSize="xsmall" onClick={this.handleToggleEmojiPicker.bind(this)}>
<bootstrap.Glyphicon glyph="heart" />
</bootstrap.Button>
<bootstrap.Overlay
show={this.state.showEmojiPicker}
onHide={this.handleClose.bind(this)}
container={content.this} //Need this of Content component
target={() => ReactDOM.findDOMNode(this.refs.targetEmojiPicker)}
>
<EmojiModalCategories actions={this.props.actions}/>
</bootstrap.Overlay>
</div>
</div>
</div>
)
}
}
【问题讨论】:
-
“所以我必须在我的
<Footer>组件中访问this的<Content>组件。” 不,相反,Footer应该接受一个回调到通知其“父级”(<View>)该按钮已被单击。然后<View>可以更新其状态、重新渲染并将不同的道具或子代传递给<Content>,使其渲染覆盖层。
标签: javascript reactjs