If condition to change route using React Router V4 If condition to change route using React Router V4 reactjs reactjs

If condition to change route using React Router V4


You can simply write a custom component that renders the Routes or redirects to the country instead of conditionally rendering the Routes which is causing this warning

const CustomRoute = (props) => {   const current = localStorage.getItem('country');   if(current) {      return <Route {...props} />   }   return <Redirect to='/countries' />}

and use it like

<Switch>        <CustomRoute exact path='/countries' component={Countries} />        <CustomRoute exact path='/industry/ranking' render={() => <IndustryRanking country={current} />} />        <CustomRoute path='/industry/audience' render={() => <IndustryAudience country={current} />} />        <CustomRoute path='/website/ranking' render={() => <WebsiteRanking country={current} />} />        <CustomRoute path='/website/audience' render={() => <WebsiteAudience country={current} />} />        <CustomRoute path='/website/device' render={() => <WebsiteDevice country={current} />} />        <CustomRoute path='/website/conversion-orders' render={() => <WebsiteConversionOrders country={current} />} />        <Redirect to='/industry/ranking' /> </Switch>