当我输入一个不存在的 url 时,我试图呈现一个组件。但是,组件会在所有路由中保持渲染。我正在使用react-router-dom@4.1.1
. 这是我设置的路线:
import * as React from "react";
import { Route, RouteComponentProps } from "react-router-dom";
import glamorous from "glamorous";
import ElementList from "./elementlist";
import AddElement from "./addelement";
import NotFound from "./NotFound";
const Styling = glamorous.div({
minHeight: 5,
minWidth: 8
});
const NavRouter = () => (
<Styling>
<Route path="/" exact={true} component={ElementList} />
<Route path="/addelement" component={(props:
RouteComponentProps<{}>) => (
<AddElement onSubmitSuccess={() => props.history.push("/")} />
)} />
<Route path="*" exact={true} component={NotFound}/>
</Styling>
);
export default NavRouter;
这是我的NotFound
组件:
import * as React from "react";
const NotFound = () => (
<h1>The page that you are looking is not there.</h1>
);
export default NotFound;
我目前面临的问题是消息:当我更改 URL 时,在路由上The page that you are looking is not there.
不断弹出。我很难尝试仅在我转到未定义的路线时才显示该消息。最初,我尝试切换路线并在顶部制作更“详细”的路线,如下所示:/
/addelement
const NavRouter = () => (
<Styling>
<Route path="/addelement" component={(props:
RouteComponentProps<{}>) => (
<AddElement onSubmitSuccess={() => props.history.push("/")} />
)} />
<Route path="/" exact={true} component={ElementList} />
<Route path="*" component={NotFound}/>
</Styling>
);
但是,它并没有解决问题。有没有办法防止消息出现在我去的每条路线上,除了未定义的路线?