Using React-Router with a layout page or multiple components per page

后端 未结 4 879
走了就别回头了
走了就别回头了 2020-11-28 19:33

I am adding react router to an existing project.

At present a model is passed in to a root component which contains a navigation component for the sub navigation an

4条回答
  •  半阙折子戏
    2020-11-28 20:21

    2019 +

    The simple and clean way to do it and avoid abusive re-rendering is (tested on react router v5, need to be confirmed on react router v4):

           
             
              
                
                
              
            
            
              
                
              
            
          
    

    which can be refactored to:

    const routes = [
      {
        layout:Layout1,
        subRoutes:[
          {
            path:"/route1/:id/:token",
            component:SetPassword
          },
          {
            exact:true,
            path:"/",
            component:SignIn
          },
        ]
      },
      {
        layout:Layout2,
        subRoutes:[
          {
            path:"/route2",
            component:Home
          },
        ]
      }
    ];
    

    with:

          
            {routes.map((route,i)=>
              r.exact)} path={route.subRoutes.map(r=>r.path)}>
                
                  {route.subRoutes.map((subRoute,i)=>
                    
                  )}
                
              
            )}
          
    

提交回复
热议问题