TypeError:store.getState不是react-redux中的函数

时间:2018-03-29 17:11:16

标签: react-redux

我按照说明从这个库中创建了商店

https://github.com/jesperorb/redux-cart

SRC /存储/ index.js:

import { createStore } from 'redux';
//Import our rootreducer from '../reducers/index.js' this hold our whole state
import rootReducer from  '../reducers/index.js';

/**
  * @param {Object} initialState
 */
export default function store() {
    return createStore(
      rootReducer,
      window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
    );
}
```

的src /动作/购物车-actions.js:

/** We don't have to use the variable 'payload', we can also just name it: 'item'
    * @param {Object} item
    */
    export function addToCart(item) {
      return {
          type: 'ADD',
          item: item
      };
    }

    export function removeFromCart(item) {
      return {
          type: 'REMOVE',
          item: item
      };
    }

的src / index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import registerServiceWorker from './registerServiceWorker';
import store from './store/index.js';


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

但我收到错误TypeError: store.getState is not a function。有人可以提出我应该首先考虑哪些步骤?

1 个答案:

答案 0 :(得分:6)

您似乎忘记在store

中创建index.js对象了
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { Provider } from 'react-redux';
import registerServiceWorker from './registerServiceWorker';
import Store from './store/index.js';

// forgot to create store obj
const store = Store();

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