I have an React Web App that collects user saved information from local storage. First I retrieve saved information to window.store. I can see in console that window.store is there. But it always gives undefined error. I tried to make sleep so that after sleep, store can get data from global window object.
init.js
const InitializeApp = () => {
let [getState, setState] = useState(false)
useEffect(()=>{
const asyncState = async() => {
let store = await loadFromStorage()
window.store = store
await sleep(5000)
console.log(store);
setState(true)
}
asyncState()
},[])
if(!getState){
return(
<InitTemplate />
)
}else{
return(
<Main />
)
}
}
App.js
const Main = () => {
return(
<Provider store={Store}>
<App />
</Provider>
);
}
store.js
const AppReducer = (state = window.store , action) => {
switch(action.type){
default :
return state;
}
}
Problem is I am getting undefined whenever I use useSelector(). How can I achieve this ?