[英]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? 有没有办法将
reduxStore
保存到全局变量,所以我可以在store.subscribe
函数之外使用?
Thank you! 谢谢!
Just assign the store you created to window
: 只需将您创建的商店分配给
window
:
const store = createStore(reducers, preloadedState);
// ...
window.reduxStore = store;
Then you can access the store from anywhere via for instance: window.reduxStore.getState().username
然后,您可以从任何地方访问商店,例如:
window.reduxStore.getState().username
However, I can't think of any valid reason for this to be necessary. 但是,我想不出有必要这样做的任何有效理由。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.