【问题标题】:Am I getting the state value correctly in Redux?我是否在 Redux 中正确获取了状态值?
【发布时间】:2017-09-08 00:29:23
【问题描述】:

我通过 redux store 的 getState() 获取组件的值。这是它的样子

handleFormSubmit (event) {
  event.preventDefault();
  // have to get this.context because my prop value doesn't update with a change in child component
  const value = this.context.store.getState();
  if (value._root.entries[3][1]._root.entries[1][1]){
    browserHistory.push('/place/' + value._root.entries[3][1]._root.entries[1][1]);
  }

要获取redux值,我们真的需要这么复杂的代码吗?

value._root.entries[3][1]._root.entries[1][1]

编辑感谢 Abdul 提到连接库。我会在这里添加代码

  constructor(props, context) {
    super(props, context)

这里是连接...

export default connect(mapStateToProps, mapDispatchToProps)(SimpleForm);

【问题讨论】:

  • 你不是没有使用 'react-redux' 提供的 Connect 和 Provider API 吗?
  • 我是!让我用connect更新答案,但我仍然不确定如何获取上下文状态。
  • @AbdulSamad 属性值不会随着子组件中的更新而更新。所以这就是我得到上下文的原因
  • 这个问题可能对你有帮助stackoverflow.com/questions/35864957/…
  • 通过连接,您将组件订阅到商店以侦听更新,并且整个商店作为道具传递给您的组件。然后你可以调整商店以获得你想要的东西。

标签: redux


【解决方案1】:

想通了!

从 getState 中使用商店的 .get

const value = this.context.store.getState();
const home = value.get('home');
const getPlace = home.get('place_id');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 2017-04-19
    • 1970-01-01
    相关资源
    最近更新 更多