React Router Switch Component Matches

后端 未结 2 1700
[愿得一人]
[愿得一人] 2021-01-13 20:59

In the react router docs here it says:

Consider this code:




        
2条回答
  •  终归单人心
    2021-01-13 21:19

    The Line

    
    

    means that everything after / will be passed into this.props.params.user variable of component and User component would be rendered.

    The matching rule only cares if the path given matches your path= pattern, it doesn't care if the resource actually exists. If I get path starting with / the and there is a text following the variable, the text will be parsed as Route Parameter user and User component will be rendered and that's it. So yes, this.props.params.user will have value of "about" in this case, but how you handle the variable and what would you display in case user such name is not found is entirely up to you.

    I think they are just trying to say that in case that you have more patterns that would normally get matched all at once, you should use component so only the first match would actually render.

    So e.g. when used :

    A) and the path is /about, rule

    
    

    would get matched and About component would get rendered and no more evaluation are done.

    B) if the path is /something, rule

    
    

    won't get matched, but rule:

    
    

    would get matched, and User component would be rendered with something as this.props.params.user param and no more evaluation are done.

    C) If the path is / the rules

    
    
    

    won't get matched but

    
    

    will and NoMatch component would get rendered.

    On contrary when not using , if your path is /about:

    
    

    Would get matched, because this rule matches all routes which paths are equal to /about.

    
    

    Would also get matched because this rule matches all routes which start with / and there is a text following.

    
    

    Would too get matched because this rule doesn't care about path at all, it gets always matched.

提交回复
热议问题