2017-06-16 348 views
0

我在我的同构反应应用程序中使用react-router-configrenderRoutes()函数。我有一个情况,就是同一个组件有很多端点。每次用户导航到新端点时,我需要为该端点提取数据并将其呈现给组件。通常我会通过componentWillReceiveProps()这样做,然后根据新的URL发起AJAX请求。但是,每次我导航到不同的端点时,组件都将被卸载并重新安装。有没有办法阻止组件被卸载,只是它的属性更新?反应路由器4 react-router-config防止组件从卸载

export const routes = [ 
{ 
    component: SectionFront, 
    path: '/' , 
    exact: true, 
    loadData: (match) =>{  
    return loadSectionFront(match.path) 
    }, 
    },{ 
    component: SectionFront, 
    path: '/investing' , 
    exact: false, 
    loadData: (match) =>{ 
    return loadSectionFront(match.path) 
    } 
}, 
{ 
    component: SectionFront, 
    path: '/news/companies' , 
    exact: false, 
    loadData: (match) =>{ 
    return loadSectionFront(match.path) 
    } 
}] 

ReactDOM.render(
    <Provider store={store}> 
     <BrowserRouter> 
      <CoreLayout> 
      {renderRoutes(routes)} 
      </CoreLayout> 
     </BrowserRouter> 
    </Provider>, 
    mountNode 
); 

回答

0

看起来问题与react-router-config本身有关。根据其renderRoutes routine的源代码,由于Switch组件包装传递的路由,它总是只会呈现一个Route

我想你将不得不为你的解决方案配置create-routes-from-config方法。