【发布时间】:2017-09-21 23:19:19
【问题描述】:
我的容器正在通过 redux store 获取状态。
我通过这样的道具将此状态传递给模态框: 示例:
render(){
let {team,id} =this.props.data;
return(
<div>
<ModalExample isOpen={this.state.isOpen} team={team} id={id}
modalClose={this.modalClose.bind(this)}
handleAddTeam={this.handleAddTeam.bind(this)}/>
</div>
)}
第一次完美运行... 模态框内有团队列表和带有添加按钮的输入字段.so, 当我在 Modalbox 组件 中执行某些添加方法并更新状态时,我可以看到 reduxDevTool 中的状态变化,甚至 state 在 mapStateToProps 中也发生了变化,但是模态框团队列表未更新或说模态框道具不会根据状态更改收到新道具...
即使在这个容器中
render(){
let {team,id} =this.props.data;
console.log(this.props.data) **//Here state change is shown**
console.log(team) **//Here state is not changed**
return(
<div>
<ModalExample isOpen={this.state.isOpen} team={team} id={id}
modalClose={this.modalClose.bind(this)}
handleAddTeam={this.handleAddTeam.bind(this)}/>
</div>
)}
另外我尝试通过这两种方式在 ModalExample 中传递道具
team={this.props.data} , team={team}
但仍然 ModalExample 视图没有更新..
困惑:如果我关闭并打开模态框或在模态框的输入字段内键入,那么根据我们的新状态,视图会发生变化...... 但我希望根据我们的 redux 状态更改即时呈现模态框视图...
【问题讨论】:
-
您可以尝试使用 componentWillReceiveProps 方法来处理 ModalExample 组件中的道具更改。或者您可以显示一些 ModalExample 的代码。
-
当我通过 ModalExample 更新 redux 存储状态时,reduxDevtool 和 mapStateToProps 清楚地显示了更改的状态,甚至 console.log(this.props.data) 也显示了更改,但问题是它没有在里面传递道具打开 modalbox(modalExample) 或者说它不是渲染组件...
标签: javascript reactjs object redux state