1

I am writing an isomorphic app with react, redux, react-router, react-router-redux. I am calling syncHistoryWithStore on client.js. But on initial load, router.locationBeforeTransitions is always null. It is populated once I navigate though.

const history = syncHistoryWithStore(browserHistory, store);
...

<Router routes={routes} history={history} />

Above code is from client.js

Should I fire LOCATION_CHANGE action on server side manually to populate initial state of react-router-redux?

4

1 Answer 1

2

You will be creating a new store on client hence the state from the store created on server is lost. We will need to pass the initial state from server to the client, so it can populate its store.

on Server:

const initialState = store.getState();

now add this to your HTML template rendered from server:

<script type="application/javascript">
  window.__INITIAL_STATE__ = ${JSON.stringify(initialState)};
</script>

on Client:

let initialState = window.__INITIAL_STATE__;

use the initialState when you createStore on Client.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. FYI, I am already doing that, but I could see that the state for react-router-redux is { locationBeforeTransitions : null}.
I asked react-router-redux team, and they say I shouldn't map locationBeforeTransitions into props. I should use locations.pathname from react-router and it worked fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.