在Redux之后将状态映射到道具不更新

时间:2018-08-24 13:37:26

标签: javascript reactjs react-native redux

我的Redux存储已正确更新,可以使用React Native Debugger看到。但是,组件内部的道具没有更新,并且未定义。

在下面的组件中,您可以看到我已正确映射到“ sessionModerator”减速器。我已经验证了这一点,在安慰this.props时可以看到该道具。

组件:

const mapStateToProps = state => {
    return {
        session: state.screenReducers.session,
        list: state.screenReducers.sessionList,
        sessionUser: state.screenReducers.sessionUser,
        user: state.sharedReducers.user,
        sessionListItem: state.screenReducers.sessionListItem,
        sessionSortOrder: state.sharedReducers.sessionSortOrder,
        sessionModerator: state.sharedReducers.sessionModerator
    };
};

我的减速器添加如下:

减速机索引文件:

import { reducer as sessionModerator } from './session/reducers/session-moderator';

export const reducers = combineReducers({
    sessionModerator: sessionModerator,
});

操作文件:

import Types from '../../../types';

export const start = () => {
  return {
    type: Types.TYPES_SESSION_MODERATOR_START,
    payload: true
  };
};

export const stop = () => {
  return {
    type: Types.TYPES_SESSION_MODERATOR_STOP,
    payload: false
  };
};

Reducers文件:

import Types from '../../../types';

export const reducer = (state = false, action) => {
  switch (action.type) {
    case Types.TYPES_SESSION_MODERATOR_START:
      return action.payload;
    case Types.TYPES_SESSION_MODERATOR_STOP:
      return action.payload;
    default:
      return state;
  }
};

在下图中,您可以看到存储已更新,因为sessionModerator的值设置为“ true”,但是操作期间实际道具的控制台未定义。

enter image description here

我尝试过的操作:

我尝试了各种事情,这些事情大多围绕状态的结构而变化,例如,我尝试在实际对象内部添加布尔值并将其值更新为对象属性,但这似乎没有用。我觉得我没有正确更新布尔值,但无法弄清楚。

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:2)

sessionModerator在调试器的screenReducers中,而不在mapStateToProps中,在sharedReducers中。 试试这个:

const mapStateToProps = state => {
  return {
    session: state.screenReducers.session,
    list: state.screenReducers.sessionList,
    sessionUser: state.screenReducers.sessionUser,
    user: state.sharedReducers.user,
    sessionListItem: state.screenReducers.sessionListItem,
    sessionSortOrder: state.sharedReducers.sessionSortOrder,
    sessionModerator: state.screenReducers.sessionModerator 
  };
};