Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: store.getState is not a function in react-redux

Tags:

react-redux

I followed the instructions and created the store from this library

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

src/store/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/cart-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();

but i get the error TypeError: store.getState is not a function. Can someone propose what steps should i look into first?

like image 704
Sokratis V Avatar asked Dec 13 '22 17:12

Sokratis V


1 Answers

Looks like you forgot to create a store object in 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();
like image 71
Alex Avatar answered Dec 31 '22 01:12

Alex