React、Redux、および React-Router 1.0.0-rc1 を使用した小さなプロトタイプがあります。プロトタイプは Webpack を使用してコード分割を行います。現在、次のように追加のルートを非同期ロードするためにgetComponents
andを使用しています。getChildRoutes
module.exports = {
path: 'donations',
getChildRoutes(location, cb) {
require.ensure([], (require) => {
cb(null, [
require('./routes/Donation'),
]);
});
},
getComponent(location, cb) {
require.ensure([], (require) => {
cb(null, require('./components/Donations'));
});
}
};
ネストされた route に到達するまで、これは正常に機能donations/:id
します。次のようになります。
module.exports = {
path: ':id',
getComponents (location, cb) {
console.log('got it', cb); // debugging
require.ensure([], (require) => {
console.log('called it', cb); // debugging
cb(null, require('./components/Donation'));
});
}
};
このルート (例: /donations/123
) に移動すると、ルートがトリガーされ、bundle.js ファイルが読み込まれ、両方console.log
の がコンソールに表示されるため、ルートがメモリに読み込まれたことがわかります。ただし、コンポーネントはマウントおよびレンダリングされません。
console.log の結果:
got it function (error, value) {
done(index, error, value);
}
called it function (error, value) {
done(index, error, value);
}
1 レベルの深さの非同期ルートは問題ありませんが、過去のネストは機能しません。コンポーネントはロードされていますが、実行されているようには見えません。
Connect
返されるコンポーネントは、次のように Redux でラップされます。
function Connect(props, context) {
_classCallCheck(this, Connect);
_Component.call(this, props, context);
this.version = version;
this.store = props.store || c…
更新: 問題は解決しました
問題は非常に単純でした。これはネストされたルートだったので、ルーターはネストされたコンポーネントを親に渡していましたがthis.props.children
、これはチェックしていませんでした。1.0.0-rc1 の (まばらな) ドキュメントを誤解していると考えてください。