Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 579
Skate to Eat Avatar asked Oct 31 '25 12:10

Skate to Eat


1 Answers

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.

like image 162
Joe Duncan Avatar answered Nov 03 '25 04:11

Joe Duncan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!