Reputation: 2834
import store from './store'
let reduxStore = ''
store.subscribe(() => {
reduxStore = store.getState().username
console.log(reduxStore) // I get USERNAME
})
console.log(reduxStore) // I get undefined
Is there a way to save reduxStore
to global variable so I can use outside of store.subscribe
function?
Thank you!
Upvotes: 4
Views: 1378
Reputation: 376
Just assign the store you created to window
:
const store = createStore(reducers, preloadedState);
// ...
window.reduxStore = store;
Then you can access the store from anywhere via for instance:
window.reduxStore.getState().username
However, I can't think of any valid reason for this to be necessary.
Upvotes: 2