我尝试恢复存储中的状态(反应,还原)

时间:2020-06-26 14:55:31

标签: javascript reactjs redux

我是React和Redux的初学者。

我来创建商店提供者和减速器。当我发货时,值在商店中发生了变化。

但是我无法在我的组件中显示此值。我一定不明白。

compenent

reducer

store

index

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

combineReducers接受您提供的所有化简器,并将其展平为单个对象。为了有效地做到这一点,它根据您提供给它的密钥为每个状态片命名空间。使用mapStateToProps时,您需要基于该密钥(在您的情况下为TestReducer)查找状态。

const mapStateToProps = state => ({
  valueTest: state.TestReducer.content,
});

为简化器命名空间通常是为了使将来的mapStateToProps代码更易于阅读/维护。在您的情况下,可能看起来像这样:

商店

export const store = createStore(
    combineReducers({
      test: TestReducer,
    },
));

mapStateToProps

const mapStateToProps = state => ({
  valueTest: state.test.content,
});