と を介してサーバー側でレンダリングするReact
とを使用してユニバーサル アプリを構築しています。Redux
NodeJS
ExpressJS
すべて正常に動作しており、Express ハンドラが呼び出され{match} from 'react-router'
、new store instance
毎回作成されます。
私が抱えている問題{renderToString} from 'react-dom/server'
は、ストアのバージョンのみをレンダリングすることです。pristine
ストアが変更された場合 (例: を介してディスパッチされたアクションcomponentWillMount
)、ストアは更新されますがgenerated markup
、 newrenderToString
が呼び出されるまでは変更されません。
- レデューサーがどのように (リクエストごとに) 状態を変更するかわからないため、 を呼び出す前に初期状態を提供することはできません
renderToString
。 - これ以上の電話は避けたいと思い
renderToString
ます。
これは私のサンプルコードです:
const store = createStore(
reducers,
// an object that provides the initial generic state
res.viewModel.__INITIAL_STATE__ || Object.create(null),
middlewares
);
// now the store is pristine and calling store.getState()
// I will retrieve an invalid version of the state
const markup = renderToString(
<Provider store={store}>
{<RouterContext {...renderProps} />}
</Provider>
);
// now the store is correctly computed and calling
// store.getState() gets the right version but the markup
// generated is still old. Only recalling `renderToString` will get
// the right markup
const markup2 = renderToString(
<Provider store={store}>
{<RouterContext {...renderProps} />}
</Provider>
);