Answer the question
In order to leave comments, you need to log in
Why nested routes don't work in react?
I do routing
import React, { useEffect, useContext } from 'react';
import { createMuiTheme } from '@material-ui/core/styles';
import { ThemeProvider } from '@material-ui/styles';
import { Switch, Route, Redirect } from 'react-router-dom';
import Loader from './UI/Loader/Loader';
import Auth from './pages/Auth/Auth';
import MainPage from './pages/MainPage/MainPage';
import { AuthContext } from './context/AuthContext/AuthContext';
const Application = () => {
const {loadingIsAuth, authorized, isAuth } = useContext(AuthContext);
const theme = createMuiTheme({ /*еще не заданы*/
darkBlue: '#1B3F7F',
//secondary: '#DB001B',
});
useEffect(() => {
authorized();
}, []);
if(loadingIsAuth) {
return <Loader />
}
return (
<ThemeProvider theme={theme}>
<Switch>
<Route exact path='/'>
{isAuth ? <MainPage /> : <Redirect to={'/login'} />}
</Route>
<Route exact path='/login' component={Auth} />
</Switch>
</ThemeProvider>
)
};
export default Application;
import React from 'react';
import { Switch, Route, Redirect } from 'react-router-dom';
import Header from '../../components/Header/Header';
import LeftMenu from '../../components/LeftMenu/LeftMenu';
import List from '../../pages/List/List';
import {useStyles} from './style';
const MainPage = () => {
const classes = useStyles();
return (
<div className={classes.wrapContent}>
<Header />
<div className={classes.bodyContent}>
<LeftMenu />
<div>
<Switch>
<Route exact path='/'>
<Redirect to={'/list'} />
</Route>
<Route exact path={'/list'} component={List} />
</Switch>
</div>
</div>
</div>
)
};
export default MainPage;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question