2016-09-18 45 views
0

我通過創建待辦事項列表來學習反應。這些清單是按項目組織的。所以我有一個projects數組和一個lists對象。 { 項目:store.projects, 名單:store.lists }有沒有辦法改變反應路由器上的商店更改?

名單會是這個樣子{projectName: ['list1',...]}

我的路線是無論我有projects陣列

const routes = ({ 
    var children = this.props.projects; 
    const routes = { 
     path: '/', 
     component: Nav, 
     indexRoute: { component: Layout }, 
     childRoutes: [ ] 
    } 
    for(let i in children){ 
     routes.childRoutes.push({path: children[i], component: Layout}) 
    } 
    return routes 
}) 
class Routing extends React.Component{ 
    render(){ 
    return(<Router className="row" routes={routes} history={browserHistory} />) 
    } 
} 

在到現在爲止還挺好。現在,如果我發送一個動作,將一個項目推入我的projects,我收到此錯誤消息:

警告:[react-router]您無法更改;它將被忽略

有沒有辦法讓路由器每次我添加一些項目時都會重新創建this.props.projects

+0

您執行此操作的方式是錯誤的,下面的@gravityplanx答案是繼續前進的正確方法。 –

回答

4

你不想爲每個產品單獨的路線。你想要的是,有一個變量在它的路徑的單一路線:

<Route path="projects/:name" component={Layout} /> 

進入這條路線就可以訪問到name值,通過this.props.params.name

相關問題