Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If condition to change route using React Router V4

I have two types of routes, countries and content:

  • /countries
  • /industry/ranking
  • /industry/audience
  • /website/ranking
  • /website/audience
  • /website/conversion

etc...

When the user enters the application, I have to verify if he already chose a country, which I'm saving using localStorage.

If the user has the country already chosen, he needs to go to /industry/ranking, if don't, to /countries.

I'm receiving a warning about the route changing by code:

<Route> elements should not change from controlled to uncontrolled (or vice versa). You provided a "location" prop initially but omitted it on a subsequent render.

My code:

<Switch>
   <Route exact path='/countries' component={Countries} />
   {
      current && (
         <React.Fragment>
            <Route exact path='/industry/ranking' render={() => <IndustryRanking country={current} />} />
            <Route path='/industry/audience' render={() => <IndustryAudience country={current} />} />
            <Route path='/website/ranking' render={() => <WebsiteRanking country={current} />} />
            <Route path='/website/audience' render={() => <WebsiteAudience country={current} />} />
            <Route path='/website/device' render={() => <WebsiteDevice country={current} />} />
            <Route path='/website/conversion-orders' render={() => <WebsiteConversionOrders country={current} />} />
         </React.Fragment>
      )
   }
   { !current ? <Redirect to='/countries' /> : <Redirect to='/industry/ranking' /> }
 </Switch>

Is there a way to improve this using just the routes to verify my condition?

Thanks!

like image 453
Italo Borges Avatar asked Sep 27 '18 15:09

Italo Borges


People also ask

How do you conditionally route in react?

Firstly wrap all the content of your page inside the return function inside the . Next, create the induvial routes inside the component. For each route, we have the path and the element props, these tell the path on the address bar and the component to be rendered out respectively.

How do you programmatically navigate using react router v4?

There are two ways to programmatically navigate with React Router - <Navigate /> and navigate() . You can get access to Navigate by importing it from the react-router-dom package and you can get access to navigate by using the custom useNavigate Hook.

How do I reroute in react router?

import { Redirect } from "react-router-dom"; The easiest way to use this method is by maintaining a redirect property inside the state of the component. Whenever you want to redirect to another path, you can simply change the state to re-render the component, thus rendering the <Redirect> component.


1 Answers

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>
like image 187
Shubham Khatri Avatar answered Oct 19 '22 18:10

Shubham Khatri