Роутинг
react-router-dom
Установка пакетов: yarn add react-router-dom @types/react-router-dom
react-router-dom v. 5
App - create router
import { BrowserRouter as Router } from 'react-router-dom';
import Routes from 'routes';
...
<Router>
<Routes />
</Router>
...
Router - routes
import { matchPath, Redirect, Route, Switch } from 'react-router-dom';
import { IRoute } from './interfaces';
import { EPermission } from './interfaces/auth';
// routes components
import {
Login,
Drugstores,
Edit,
Help,
MassEdit,
Map,
MapOld,
CustomStatuses,
PermissionsRoute,
Regions,
} from 'container';
export const AUTH = 'login';
export const MAIN = 'main';
export const MAP = 'map';
export const MAP_TEST = 'map-test';
export const EDIT = 'edit';
export const MASS_EDIT = 'mass_edit';
export const STATUSES = 'statuses';
export const HELP = 'help';
export const REGIONS = 'regions';
export const routes: IRoute[] = [
{
id: AUTH,
path: `/${AUTH}`,
exact: true,
permissions: [EPermission.NONE],
children: <Login />,
},
{
id: MAIN,
path: '/',
exact: true,
permissions: [EPermission.NONE],
children: <Drugstores />,
},
{
id: MASS_EDIT,
path: '/mass_edit',
exact: true,
permissions: [EPermission.NONE],
children: <MassEdit />,
},
{
id: EDIT,
path: '/edit/:id',
exact: true,
permissions: [EPermission.NONE],
children: <Edit />,
},
{
id: MAP,
path: '/map',
exact: true,
permissions: [EPermission.NONE],
children: <Map />,
},
{
id: MAP_TEST,
path: '/map-test',
exact: true,
permissions: [EPermission.NONE],
children: <MapOld />,
},
{
id: STATUSES,
path: '/statuses',
exact: true,
permissions: [EPermission.NONE],
children: <CustomStatuses />,
},
{
id: HELP,
path: '/help',
exact: true,
permissions: [EPermission.NONE],
children: <Help />,
},
{
id: REGIONS,
path: '/regions',
exact: true,
permissions: [EPermission.NONE],
children: <Regions />,
},
];
const Routes = () => {
return (
<Switch>
{routes.map(props => {
return <PermissionsRoute key={props.id} {...props} />;
})}
<Route path='*'>
<Redirect to='/' />
</Route>
</Switch>
);
};
export default Routes;