react-router render menu when path does not match

后端 未结 6 1752
一向
一向 2020-12-20 11:36

I\'m using react-router and I want to render a menu component when the user is not in the root and not in the /login path. This is what I have so far

6条回答
  •  被撕碎了的回忆
    2020-12-20 12:05

    Simplest Implementation

    Use a ternary expression or short-circuit evaluation to conditionally render your component based on location.pathname, like so:

     ['/', '/login'].includes(location.pathname)
            ? 
            : null
        }
    />
    

    Regex Implementation

    React Router's matching of path strings relies on path-to-regexp@^1.7.0.

    As a result, you can instruct routes to not render for certain paths using regular expressions.

    The following implementations should render given any path value, bar "/" and "/login":

    // With Regex Inside String.
    
    
    // With Explicit Regex.
    
    

提交回复
热议问题