Skate to Eat
Skate to Eat

Reputation: 2834

Redux save store as global variable on React app

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

Answers (1)

Joe Duncan
Joe Duncan

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

Related Questions