Jitender
Jitender

Reputation: 7971

Not able to access store on non react component

The redux getState method is not available. Do I need any middleware for achieving the desired result. Following is the code

const configureStore = (initialState = {}) => {
  return createStore(
    rootReducer,
    initialState,
    applyMiddleware(...middleware),
  );
};

export default configureStore;

util.js

import store  from '../../store';

store.getState()

// _store2.default.getState is not a function

Upvotes: 0

Views: 382

Answers (2)

Tyler Sebastian
Tyler Sebastian

Reputation: 9448

Looks like you intended to, at some point, initialize the store.

I'm not sure how you want to layout your app, but

// index.js

import configureStore from 'store';

export const store = configureStore({ ... someInitialState });

then

// util.js

import store from 'index';

// use store

I don't think this is the best solution - instead of exporting an store initializer from store, just initialize the store and export the store.

Upvotes: 0

Adam
Adam

Reputation: 3518

You are returning a function instead of your store. Seems like you have mixed up reducers with the store. What you want is this:

const store = createStore(
    rootReducer,
    initialState,
    applyMiddleware(...middleware),
);


export default store;

Upvotes: 4

Related Questions