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.
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.
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.
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.
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.
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>
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With