Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have multiple <Switch> in React.js?

I am building a React project without Redux.

I would love to have two, or in this case 3 different Switches

1st Switch will be able to Switch between Home page (normal website) and UserPage (Dashboard) when user is logged in... Then each of this will be Switchers as well, so Home will Switch among Home components, and UserPage will switch among UserPage components. Is this even possible?

                        Main Switcher 
     Home Page (Switch)              Dashboard
Home About Contact, Careers     My Profile, Courses, Classes, Donations...

This is how project should look like and should be structured.

like image 562
Amel Amcë Muminovic Avatar asked Mar 10 '18 11:03

Amel Amcë Muminovic


People also ask

How do you define a switch in React JS?

Switches toggle the state of a single setting on or off. The switch is useful when we want users to toggle between any value, like iPhone Silent Button, etc. Material UI for React has this component available for us and it is very easy to integrate.

Can a React app have more than one page?

The use cases for having multiple pages in a single React app are pretty simple. You can create a website, and easily classify different types of content on different pages. But, it should also be understood that the default implementation of React is made to use a single HTML file, and this is by design.

How do I switch between two pages in React JS?

To switch between pages in React, we can use the React Router package. First, we create the Foo and Bar page components. Next, we add the Route s to map the URLs to the components to render. Then we add the NavLink s to add links that lets us set the class name of the active link.

Why we use switch in React?

By using this exact prop, we make sure that the home route is only rendered when the relative URL exactly matches /. Now if the relative URL is /profile then only the profile route is rendered doesn't matter if the profile Route is below / route in the code.


1 Answers

You can use as many Switch components as you want. It simply renders the first match of all the routes specified under it.

Something along these lines should work, in your case:

const Home = ({match}) => {
  return(
    <Switch>
      <Route path={match.url} exact={true} component={HomeDefaultComponent} />
      <Route path={`${match.url}/about`} exact={true} component={About} />
      <Route path={`${match.url}/contact`} exact={true} component={Contact} />
      <Route path={`${match.url}/careers`} exact={true} component={careers} />
    </Switch>
  );
};

const Dashboard = ({match}) => {
  return(
    <Switch>
      <Route path={match.url} exact={true} component={DashboardDefaultComponent} />
      ... other Dashboard paths like Home component above
    </Switch>
  );
};

const App = () => {
  return(
    <Switch>
      <Route path='/home' component={Home} />
      <Route path='/dashboard' component={Dashboard} />
    </Switch>
  );
}
like image 87
Shishir Avatar answered Oct 11 '22 16:10

Shishir