我的第一个反应项目的第一个问题。reactjs重置组件的道具将接收作业
我想弄清楚如何在特定的onClick链接上最好地实现组件,以便能够重新触发此效应并且不会受到页面上其他链接的影响。前两个问题正在工作,但我似乎没有其他链接不影响组件。
我有一个DisplayLightbox组件我的网页接受一对夫妇值
<div>
<DisplayLightbox
showLightbox = {this.state.showLightbox}
lightboxValue = {this.state.travelCity}
/>
</div>
我想触发灯箱被调用设置状态(和发送道具)的功能链接。这部分似乎工作正常。
onClick={() => this.showLightbox(el.field_city)}
showLightbox(travelCity){
this.setState({
showLightbox: true,
travelCity: travelCity,
});
}
以我DisplayLightbox部件,所述componentWillReceiveProps确实设置状态为真,这增加了磅活性类在div,其中,从所述的CSS,显示在灯箱的div。这看起来很好。
class DisplayLightbox extends React.Component {
constructor(props) {
super(props);
this.state = {
showLightbox: false,
};
}
componentWillReceiveProps(nextProps) {
if (nextProps.showLightbox !== this.state.showLightbox) {
this.setState({ showLightbox: nextProps.showLightbox });
}
}
closeLightbox() {
this.setState({
showLightbox: false,
});
}
render() {
var lbActive = (this.state.showLightbox === true) ? "lb-active" : ""
return <div className={"lightbox " + lbActive }>
<div className={"lightbox-close " + lbActive } onClick={() =>
this.closeLightbox()}>CLOSE</div>
Copy in lightbox
</div>;
}
}
寻找到它,我看到的是,由于道具不是由组件控制和只读,一旦它被设置为True,并且我通过设置showLighbox的状态恢复到假,nextProps关闭股利。 showLightbox保持真实。所以,如果我关闭它(closeLightbox)并单击我的页面上的其他onClick,它仍会查看我的组件,看到nextProps.showLightbox仍设置为TRUE并打开Lightbox。
我只想让灯箱打开,如果该特定链接是被点击的人。看起来每个其他链接都将showLightbox的状态设置为false,所以我猜测我没有正确地看待它。
感谢
非常感谢您的快速回复。我已经添加了一个按钮div来切换showLightbox状态,并且正如您所说,我可以从子项中移除componentWillReceiveProps。似乎工作。 – user1932694