I want to split my app in 2 different parts.
An exam list should link to a student list.
import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Link, Route, Switch, useRouteMatch } from 'react-router-dom';
// doesn't work
const ExamList = () => {
return (
<p>
current: ExamList ,<Link to='/students'>to Students</Link>
</p>
);
};
// nested router
const Exams = () => {
let { path, url } = useRouteMatch();
return (
<BrowserRouter>
<Switch>
<Route exact path={path}>
<ExamList />
</Route>
</Switch>
</BrowserRouter>
);
};
// worked
const Students = () => {
return (
<p>
current: Students ,<Link to='/'>to Exams</Link>
</p>
);
};
ReactDOM.render(
<BrowserRouter>
<Switch>
<Route exact path='/'>
<Exams />
</Route>
<Route path='/students'>
<Students />
</Route>
</Switch>
</BrowserRouter>,
document.getElementById('root')
);
/ click the link [to Students] doesn't render Students component, but if I refresh the browser, the Students component will be rendered currectly.
/students click the link [to Exams] worked fine.