【问题标题】:redux Didnt Store in each stateredux 在每个状态下都没有存储
【发布时间】:2020-01-05 23:05:05
【问题描述】:

我在 ComponentDidMount 中调用了 3 个操作,但结果是 保存在最后一个被调用的动作中

  componentDidMount() {
    if (this.props.auth.token !== undefined) {
      this.props.getBillsRequest(this.props.auth.token)
      this.props.getProfile(this.props.auth.token)
      this.props.getInstallmentsRequest(this.props.auth.token)
      console.log(this.props)
    } else {
      this.props.navigation.navigate("Login")
    }
  }

预期的输出是

bills : {items: object}
profile: {user: object, detail: object}
installments: {0: object, 1:object , ...}

但我得到了

bills: {items: undefined}
profile: {user: undefined}
installments: {items: object, profile: object, biils: object}

【问题讨论】:

  • 能否请您先将所有逻辑移出reducer,并按照标准保持纯函数?
  • 不要发图片;发布一个最小的代码示例。看看writing the perfect question
  • 无法在 utils 中移动逻辑

标签: javascript react-native redux


【解决方案1】:

您需要 await 进行 fetch 调用,而不是在声明要运行的承诺后直接返回 newstate,不这样做会导致 newstate 立即返回,因此它处于初始状态。但是不要这样做,你的 redux reducer 应该是纯的。

你不应该在你的 reducer 中进行 ajax 调用。详情请见Perform Ajax Fetch in a Redux Reducer?

我建议调整一些模式来处理 redux。我非常喜欢 Nir ​​Kaufman 概述的模式,相关代码请参阅 https://www.youtube.com/watch?v=JUuic7mEs-shttps://github.com/isubasinghe/advanced-redux-patterns

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-03
    • 2016-09-27
    • 2018-03-05
    • 2016-04-21
    • 2013-10-19
    • 2020-05-13
    • 2020-07-22
    • 2020-01-14
    相关资源
    最近更新 更多