我正在使用react-router
v4來生成從異步API中加載的動態路由。從API檢索到的數據通過其將創建陣營路由函數通過,類似於:在Redux中存儲React路由
import Home from "containers/home"
import FeaturePage from "containers/featurePage"
response.map((item) => {
if (item.value.toLowerCase() === "home") {
return {
path: item.path,
component: Home,
exact: item.isExact
}
} else if (item.value.toLowerCase() === "featurePage") {
return {
path: item.path,
component: FeaturePage,
exact: item.isExact
}
} else {
// ...etc
}
});
一旦路由被加載,它們被使用動作減速器照常加入到Redux的存儲。這在redux商店支持功能時效果很好。
但是在Redux商店doesn't seem to be the best way forward中存儲函數。什麼是更好的實施?我需要存儲需要渲染的組件,這是不可序列化的。
我設法通過在存儲中存儲組件名稱然後將組件名稱傳遞給返回組件的函數來實現此目的。我需要這個組件,以便在創建react-router組件時引用它。來自 「容器/家」 常量getRouteComponent =名=> { 開關(名稱){ 情況下 「主頁」 ''' 導入主頁: 返回首頁; 默認值: return undefined; } }; ''' –
如果你能夠更新你的例子,我可以把它標記爲正確的。 我創建'使用反應-router'路線: {routes.map(路線=> { 回報( <路線 關鍵= {} route.path確切 路徑 = {route.path} 部件= {getRouteComponent(route.component)} /> ); }}} –