2

I would like to use code I am already using little bit differently. I have critical data stored in the state which has to be available straight after page reload, otherwise users will get logged out etc. To achieve this I am using this kind of function

window.addEventListener('onbeforeunload', store.dispatch(setDataAfterBrowserRefresh()));

Inside that function though, I would like to be able to access the state how it is/was just before the page reload/refresh. (Currently I am kind of only reloading everything again) So I am asking is it a bad practice to write something like this inside my app.jsx and another file:

app.jsx

export const store = createStore(
connectRouter(history)(rootReducer),
{},
composeEnhancers(
applyMiddleware(
routerMiddleware(history),
thunk))
);

BrowserRefreshHandlerFile.js

import { store } from '../app.jsx';

some code in between and then

store.getState().partOfStateIWantToAccess

I have gotten used to access the store only by using props inside connected components and by using action dispatchers and all that, and not "directly" within some js code.

Any comments and thoughts are welcome.

Mirko Flyktman
  • 248
  • 2
  • 13

1 Answers1

1

Aside from the fact that React applictions should not be reloaded, I advise you to take a look at this: How can I persist redux state tree on refresh?

There is a link to middleware that keeps your state on a reload.

Craws
  • 363
  • 1
  • 19
  • I found that SO post earlier, and it seems that the middleware mentioned there are not maintained anymore. Also I don't really know how I am supposed to prevent reloading a web page, and that is anyway something I am not going to be able to implement, so I have to have solution for keeping at least some parts of the state intact. – Mirko Flyktman Nov 21 '18 at 10:07