我正在写一个与react,redux,react-router,react-router-redux同构的应用。我要给client.js上的syncHistoryWithStore打电话。但在初始加载时,router.locationBeforeTransitions始终为空。一旦我导航,它就会被填充。
const history = syncHistoryWithStore(browserHistory, store);
...
<Router routes={routes} history={history} />上面的代码来自client.js
我应该在服务器端手动启动LOCATION_CHANGE操作来填充react-router-redux的初始状态吗?
发布于 2016-11-21 09:14:10
您将在客户端创建一个新的存储,因此在服务器上创建的存储的状态将丢失。我们需要将初始状态从服务器传递到客户端,以便它可以填充其存储区。
服务器上的:
const initialState = store.getState();现在将此代码添加到从服务器呈现的HTML模板中:
<script type="application/javascript">
window.__INITIAL_STATE__ = ${JSON.stringify(initialState)};
</script>客户端上的
let initialState = window.__INITIAL_STATE__;当您在客户端使用createStore时,请使用initialState。
https://stackoverflow.com/questions/40711199
复制相似问题