I'm building a react app and I can't make the routing work.
I need one common layout (header, footer) for a few Auth routes (
/login
,sign-up
,forgot-password
, etc...)And I need need another common layout for the rest of the protected parts of the app (
Home
,Dashboard
, etc...)I need another 404 page without any layout.
I've tried several techniques from those links:
- Multiple Layouts with React Router v4
- https://simonsmith.io/reusing-layouts-in-react-router-4
- Nested routes with react router v4 / v5
- https://reacttraining.com/react-router/web/example/route-config
But could reach working version.
This is what I'm currently have:
(Note: for now I'm ignoring the need to block non logged-in users into the private routes of AppLayout, I'll handle that right after)
const App: React.FC = () => {
const history = createBrowserHistory();
return (
<div className="App">
<Router history={history}>
<Switch>
<AppLayout>
<Route path="/home" component={HomePage}/>
<Route path="/dashboard" component={DashboardPage}/>
...
</AppLayout>
<AuthLayout>
<Route path="/login" component={LoginPage}/>
<Route path="/sign-up" component={SignUpPage}/>
...
</AuthLayout>
<Route path="*" component={NotFoundPage} />
</Switch>
</Router>
</div>
);
};
export default App;
Both AuthLayout
and AppLayout
are simple and similar to that (just with different header/footer for each):
class AppLayout extends Component {
render() {
return (
<div className="AppLayout">
<header>...</header>
{this.props.children}
<footer>...</footer>
</div>
);
}
}
export default AppLayout;
The problem is that only routes from the AppLayout are rendered.
Other routes just showing the AppLayout header
and footer
without any content.
Those are the react versions I'm using:
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^5.0.0",
Any help would be appreciated.
Thanks in advance.