【问题标题】:get latest state before dispatch react-navigation & redux在调度 react-navigation 和 redux 之前获取最新状态
【发布时间】:2017-07-19 09:14:23
【问题描述】:

我只是想知道我是否使用 redux 调用 dispatch 并发送 props 并导航到同一函数中的另一个组件,如何确保在渲染组件之前获得最新的 props?

................

onSubmit() {
  const { navigate } = this.props.navigation;
  this.props.correctAnswer(30);
  
  navigate('Another_Component',{score: this.props.totalScore});
}

return {

render(
  <View>
    <TouchableOpacity onPress={() => this.onSubmit()}>
      <Text>ADD 30</Text>
    </TouchableOpacity>
  </View>
);

}

const mapStateToProps = (state) => {
  return {
    totalScore: state.CurrentActScore
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    correctAnswer: (data) => {
      dispatch({ type: 'ADD_SCORE', value: data})
    }
  }
}

...........

现在它仍然发送 0,初始状态。

当我添加一个 componentWillReceiveProps() 时,它仍然使用旧值导航.. 我认为在它完成过程和 setState 之前,它已经导航了。

【问题讨论】:

    标签: react-native redux react-redux react-navigation


    【解决方案1】:

    您需要 subscribe() 到您的商店,然后 onStoreUpdate() 您需要 getState 并将其分配给您的组件 this.state 或 this.props

    获得最新状态后,您可以使用最新数据调度/setState。

    【讨论】:

      猜你喜欢
      • 2020-12-05
      • 1970-01-01
      • 2023-03-22
      • 2020-10-26
      • 2017-08-28
      • 2018-05-28
      • 2018-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多