未设置Redux状态

时间:2020-06-24 15:40:25

标签: reactjs redux react-redux

我正在尝试使用Redux存储创建可设置用户ID的React应用程序。这是我的:

减速器:

const cmsDefaultState = {
  cmsNum: "",
};

export default (state = cmsDefaultState, action) => {

  switch (action.type){

    case 'SET_CMS_ID':
    console.log("triggered");
      return Object.assign({}, state, {
        cmsNum: action.cmsNum
      });

    default:
      return state;
  }

};

方法:

export const setCMSId = (cmsNum = '') => ({
  type: 'SET_CMS_ID',
  cmsNum
});

在我的index.js中,这就是我所拥有的:

import { Provider, connect } from 'react-redux';
import configureStore from './redux/configureStore.js'

import { setCMSId } from './redux/methods.js'
const store = configureStore();

store.dispatch(setCMSId({cmsNum: "Hello"}));
console.log("[" + store.getState().cmsNum + "]");

ReactDOM.render(
  <React.StrictMode>
  <Provider store={store}>
      <App />
    </Provider>
  </React.StrictMode>,
  document.getElementById('root')
);

因此从本质上讲,在console.log上,我应该已经打印了[Hello],但实际上它正在打印[[object Object]]

有什么想法吗?不确定我哪里出错了吗?

0 个答案:

没有答案