【问题标题】:Where does the mapStateToProps state comes from?mapStateToProps 状态从何而来?
【发布时间】:2019-10-04 09:00:39
【问题描述】:

我是 React Native 的新手,目前正在维护一个内置 ReactNative 的应用程序。在我现有的应用程序中,我们使用 combineReducers 和 reduxSause。根据我在互联网上的发现,combineReducers 将为每个减速器管理一个单独的状态(即在下面的示例中,苹果将具有单独的状态,香蕉将具有单独的状态)

const rootReducer = combineReducers({
  a: apples,
  b: bananas
});

在页面屏幕中,我也在使用 mapStateToProps 和 mapDispatchToProps。我的问题是 mapStateToProps 的状态来自哪里?因为当我给appleReducer调用Dispath回调函数时,那边的状态是空的。

const mapStateToProps = (state) => {
      const { data } = state.bstate
}

const mapDispatchToProps = (dispatch) => ({
      getLine:(input) => dispatch(BType.getLine(input)),
})

【问题讨论】:

    标签: react-native redux react-redux


    【解决方案1】:

    您需要使用react-redux 库中的connect 函数,它是使用mapStateToPropsmapDispatchToProps 并向它们注入状态的connect 函数:

    https://react-redux.js.org/api/connect

    【讨论】:

    • 嗨@lavor,谢谢,但我的问题是它代表哪个州?因为当我调用reducor的函数时,reducer中的状态是空的(其中mapStatetoProps状态上有一些值)
    • 表示组合状态{ a: apples, b: bananas}。因此,mapStateToProps 的状态参数应该具有该组合状态对象。当您使用您的状态时,您能否发布 mapStateToProps、mapDispatchToProps 和代码的代码,以及您如何调度操作 + 操作本身的代码?因为我确实从中理解了这个问题:“当我调用减速器的函数时,减速器中的状态为空”。
    • 您好,感谢您的回复。我找到了解决方案。在当前的应用程序中,老开发人员只保留 B 状态,并且在 mapStateToProps 中我只得到 B 状态。当我调用 Apple 的 reducer 函数时,它是空的,因为我们不在这里持久化数据,
    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 2020-11-14
    • 1970-01-01
    • 2019-11-15
    • 2017-05-31
    • 2019-11-10
    • 2013-02-12
    • 2020-06-26
    相关资源
    最近更新 更多