0

我正在尝试在反应应用程序中使用 redux。我已经设置了 redux 并配置了 store,并将 store 提供给了包装整个应用程序的 Provider。我无法从商店访问任何财产。

我的 Index.js 如下

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

import { Provider } from 'react-redux';

import store from './redux/store';

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

reportWebVitals();

store.js

import { createStore } from 'redux';

import mainReducer from './Reducer';

const store = createStore(mainReducer);

export default store;

减速器

import { Types } from './Types';

const INITIAL_STATE = {
  formData: {
    projectName: '',
    projectDescription: '',
    client: '',
    contractor: '',
    min_x: undefined,
    max_x: undefined,
    min_y: undefined,
    max_y: undefined,
    min_z: undefined,
    max_z: undefined,
  },
  step: 1,
  hasResult: false,
};

const mainReducer = (state = INITIAL_STATE, action) => {
  switch (action.type) {
    case Types.SET_STEP:
      return {
        ...state,
        step: action.payload,
      };
    default:
      return state;
  }
};

export default mainReducer;

当我尝试使用 mapStateToProps 从减速器访问任何属性时,如下所示

const mapStateToProps = (state) => ({
  step: state.mainReducer.step,
});

我收到一条错误消息

TypeError:无法读取未定义的属性“步骤”

我在这里做错了什么?

4

0 回答 0