The reason for this behavior is that the Switch
component uses the path
and from
props of its children components to check whether there is a route match, which decides what is rendered.
The ITRoleRouter
component instances don't have these props but are children of the Switch
component.
Because both ITRoleRouter
instances don't have these props it will instead match the first instance.
So instead you could do something like this:
interface ITProffesion {
path: string;
proffesion: string;
}
const ITRoleRouter: React.FC<ITProffesion> = ({ path, proffesion }) => (
<Route exact path={path} key={proffesion}>
<p>Works {proffesion}</p>
</Route>
);
const App: React.FC<{}> = () => (
<Router>
<Switch>
<Route exact path="/">
<DemoContainer />
</Route>
<ITRoleRouter path="/backend" proffesion="backend" />
<ITRoleRouter path="/frontend" proffesion="frontend" />
</Switch>
</Router>
);
Check out this Github issue for more info.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…