React conditionally render a component in specific routes

前端 未结 1 1529
迷失自我
迷失自我 2021-01-28 01:54

This is my Index.js

ReactDOM.render(
   
        
            
                       


        
相关标签:
1条回答
  • 2021-01-28 02:24

    I haven't tested it, just brought it up from the top of my head. The idea is that you create a map of routes where your BottomNav should appear. Else, just return null.

    import {withRouter} from 'react-router-dom'
    
    const ROUTES_WITH_BOTTOM_NAV = {
       '/home': true,
       '/profile/friends': true
    }
    
    const BottomNav = (props) => {
       const currentPage = props.location.pathname;
       if (!ROUTES_WITH_BOTTOM_NAV[currentPage]) return null;
    
       return (...yourBottomNavJsx)
    }
    
    export const withRouter(BottomNav)
    

    Let me know if that helped.

    0 讨论(0)
提交回复
热议问题