Redux stores storage as a global variable in 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 store reduxStore

for a global variable so I can use outside of the function store.subscribe

?

Thank!

+3


source to share


1 answer


Just assign a value to the created store window

:

const store = createStore(reducers, preloadedState);

// ...

window.reduxStore = store;

      



Then you can access the repository from anywhere via for example: window.reduxStore.getState().username

However, I cannot think of any compelling reason for this.

+1


source







All Articles