私は最近、この目的のために特別にライブラリをリリース:https://github.com/elliottsj/react-router-query
それは、非同期の経路(すなわちgetChildRoutes
/getIndexRoute
)を処理し、だけではなく、配列を提供することができますパス(fullPath
)を含む「FlatRoute」オブジェクトの配列と、各ルートのすべての元のプロパティと、「親」ルートの配列:
fullPath: '/'
も
fullPath: '/author'
には「FlatRoute」オブジェクトがありません。唯一の「葉」ルートが結果に含まれ
import { query } from 'react-router-query';
const routes = (
<Route path="/" component={App}>
<Route path="author" component={Author}>
<Route path="about" component={About} />
</Route>
<Route path="users" component={Users} />
</Route>
);
query('/', routes, (error, result) => {
expect(result).toEqual([
{
fullPath: '/author/about'
component: About,
parents: [
{ path: '/', component: App },
{ path: 'author', component: Author },
],
},
{
fullPath: '/users'
component: Users,
parents: [
{ path: '/', component: App },
],
},
]);
});
注意。これは、子ルートのレイアウトとしてのみ使用されるルートを含むことを防止するためです。
<Route path="/" component={App}>
<Route path="posts" component={PostLayout}>
<Route path="1" component={Post1} />
<Route path="2" component={Post2} />
<Route path="3" component={Post3} />
</Route>
</Route>
あなたが/posts
ためFlatRouteをしたい場合は、単にIndexRoute
を含める:
<Route path="/" component={App}>
<Route path="posts">
<IndexRoute component={Posts} />
<Route path="1" component={Post1} />
<Route path="2" component={Post2} />
<Route path="3" component={Post3} />
</Route>
</Route>
このためのユースケースは何ですか? – eenagy
静的なサイト生成のためにwebpack用のプラグインを使用したいと思います。要するに、私はgithub.ioで私のアプリを公開したいと私は反応ルータを使用しています。このプロジェクトを参照してください:https://github.com/markdalgleish/static-site-generator-webpack-plugin。彼らは、ルートの配列がエントリとして期待しています。 –
ここでサイトマップ[react-router-sitemap](https://www.npmjs.com/package/react-router-sitemap)を生成するものですが、私はあなたが単純に減らす方が良いと思いますあなたがより複雑なルートを持つまで、 – eenagy