I'm having a hard time pre-loading state with React / Redux.
Before marking this as duplicate, I've already read these:
- http://redux.js.org/docs/recipes/reducers/InitializingState.htm
- Redux - managing preload state
- What are your best practices for preloading initialState in your Redux apps?
Here's my scenario:
- I'm using Node + React + Redux.
- When I load a page, from the server, I load
User
object from the database. I need thisUser
object to be in the state without a roundtrip to the server. - Because the
User
object is dynamic, I can't have it in the bundle. - I'm not using server-rendering.
My problem:
How do I initially load the User object from the server into my store in the Client?
In this answer, Dan Abramov says:
On the client, you’d read it from a global variable, and create the client store instance with it. In other words you never have to manually create initialState—you’re supposed to grab it on the server with store.getState(), pass it to client, and create a store with it.
So, I'm assuming that, by global variable, he means something like this:
// Here, window._initialState.user is being sent to the client in a <script/> tag.
const store = createStore(reducers, { user: window._initialState.user);
The above might work, the problem is that I don't have confidence that this is actually a good practice because I didn't find any example on the internet about it.
So, Is this the right thing to do? Is there a better / more recommended way of doing it?