2016-11-21 14 views
1

私はreact、redux、react-router、react-router-reduxで同形アプリを書いています。 私はsyncHistoryWithStoreにclient.jsを呼び出しています。しかし、初期ロード時には、router.locationBeforeTransitionsは常にnullです。それは私が一度移動すると設定されます。上記のコードreact router redux同形アプリの初期状態設定

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

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

client.js

からである私が反応し-ルータ再来の初期状態を設定するために手動でサーバー側のLOCATION_CHANGEアクションを発射すべきか?

+0

offtopic少しでも、私はあなたがそれを行う上で持っているすべてのリソースを共有することができれば同形的で好奇心を持っていますか? –

+1

私はこの定型文から分岐しました。 https://github.com/CrocoDillon/universal-react-redux-boilerplate – sean

+1

また、https://blog.tableflip.io/server-side-rendering-with-react-and-redux/で詳しく説明しています – sean

答えて

2

クライアントに新しいストアを作成するため、サーバー上に作成されたストアの状態が失われます。最初の状態をサーバーからクライアントに渡す必要があるため、ストアにデータを格納できます。サーバー上の

:クライアント

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

const initialState = store.getState(); 

は、今のサーバーからレンダリングしてHTMLテンプレートにこれを追加し

let initialState = window.__INITIAL_STATE__; 

はを使用クライアントの場合はcreateStoreです

+0

あなたの答えをありがとう。参考までに、私はすでにそれをやっていますが、react-router-reduxの状態は '{locationBeforeTransitions:null}'です。 – sean

+1

私はreact-router-reduxチームに尋ねました。彼らは、私がlocationBeforeTransitionsをpropsにマップすべきではないと言います。私はreact-routerから 'locations.pathname'を使うべきで、うまくいきました! – sean

関連する問題