Nested routes with react router v4 / v5

后端 未结 11 2072
广开言路
广开言路 2020-11-22 12:34

I am currently struggling with nesting routes using react router v4.

The closest example was the route config in the React-Router v4 Documentation.

I want t

11条回答
  •  我在风中等你
    2020-11-22 13:28

    Using Hooks

    The latest update with hooks is to use useRouteMatch.

    Main routing component

    
    export default function NestingExample() {
      return (
        
          
           
             
           
         
        
      );
    }
    

    Child component

    function Topics() {
      // The `path` lets us build  paths 
      // while the `url` lets us build relative links.
    
      let { path, url } = useRouteMatch();
    
      return (
        

    Topics

    /topics/otherpath/
    • /topics/topic1/
    • /topics/topic2
    // You can then use nested routing inside the child itself

    Please select a topic.

    ); }

提交回复
热议问题