【发布时间】:2019-03-13 18:29:27
【问题描述】:
我使用react-navigation 在两个屏幕之间导航,同时在它们之间传递数据。
流程: 屏幕 A(传递数据)-> 屏幕 B ->(更新并传回数据)屏幕 A。
在屏幕 A 中,我正在使用一个子组件,在从屏幕 B 接收到数据时需要对其进行更新/重新渲染。
我检查了数据是否正确传递,并且我确信主屏幕中子组件正在使用的状态正在更新。我只是想不通为什么在读取 state 的新值后子组件没有重新渲染?
主屏幕:
updateCount(data) {
// Logic to update state of a counter variable
this.setState({ count: data })
}
// and then later on in the code, I'm calling the child Component 'B'
<B prop={this.state.count} />
组件 B:
componentWillMount() {
// based on value of this.props.count, I'm setting the value of 'text' in this component
if(this.props.count == 1) {
this.setState({text: 'abc'})
} else {
this.setState({text: 'cde'})
}
}
// later on in the code, render method:
<View>
<Text>
{this.state.text}
</Text>
</View>
【问题讨论】:
-
尝试将 componentWillMount 更改为 componentDidMount。另外,你能创建一个codesandbox吗?
标签: reactjs react-native react-navigation