Вы можете Redirect
к определенному маршруту, если не найдено ни одного маршрута, а затем проверить этот маршрут в Header
, чтобы скрыть этот компонент.
<Switch>
<Route path="/money-transfer" component={MoneyTransfer} />
<Redirect from="/" to="/money-transfer" exact />
<Route path="/dmt" exact component={MyDmt} />
<Route component={NoFound} />
</Switch>
function NoFound() {
return <Redirect to="/notfound" />;
}
// Header.js
const Header = props => {
const { location } = props;
if (location.pathname.match(/notfound/)) {
return <NotFoundRoute />;
}
return <h3>I am the Header</h3>;
};
export default withRouter(Header);
// NotFoundRoute.js
function NotFoundRoute() {
return <div>No Route Found</div>;
}
Пример рабочего кода коды кодовой ссылки
Надеюсь, это поможет !!!