【问题标题】:Redux batch doesn't work properly on React Native appRedux 批处理在 React Native 应用程序上无法正常工作
【发布时间】:2021-11-03 05:57:47
【问题描述】:

需要调用多个调度。尝试使用 react-redux 中的批处理,但它仅触发函数内的第一个调度。尝试交换调度,同样,仅触发第一个,在开发工具中检查。 Thunk 已连接

应用内:

  dispatch(
    setData({
      user,
      userLang,
      userToken,
      dateMargin,
    }),   )

行动:

export const setData = ({user, userLang, userToken, dateMargin}) => {
  return dispatch => {
    batch(() => {
      dispatch(setToken(userToken))
      dispatch(setLang(userLang))
      dispatch(setUser(user))
      dispatch(setDateMargin(dateMargin))
    })
  }
}

【问题讨论】:

    标签: reactjs react-native redux react-redux


    【解决方案1】:

    是的,batch 是从react-dom 重新导出的。 React-native 没有类似的 API,所以那里是不可能的。

    一般情况下,建议使用Model Actions as Events, Not SettersAllow Many Reducers to Respond to the Same ActionAvoid Dispatching Many Actions Sequentially(有关更多说明,请参阅链接),因此您在这里所做的几乎是一种反模式:
    你的组件中有逻辑,尽管 Redux 的强大之处在于它将状态逻辑从你的组件中移出,进入你的 Store。

    按照官方 Redux 样式指南的建议,您更愿意

      dispatch(userLoggedIn({
        userToken,
        userLang,
        user,
        dateMargin
      })
    

    并且可能有多个 reducer 作用于该事件类型的操作。

    【讨论】:

      猜你喜欢
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      • 2014-10-23
      • 2020-04-12
      • 1970-01-01
      • 2018-06-13
      • 2021-10-01
      • 1970-01-01
      相关资源
      最近更新 更多