React-Router:如何检索当前路由的父级?

Fra*_*ran 5 reactjs react-router

如何检索当前路由的父级?我需要它才能在我的应用程序中“向上”导航。

haz*_*ous 1

在子组件的渲染中 -

var currentPath = this.props.location.pathname,
parentPath = currentPath.substring(0, currentPath.lastIndexOf("/"));
return (<Link to={parentPath}>Up</Link>...);
Run Code Online (Sandbox Code Playgroud)

  • 这是假设您为每个“/”嵌套路由。参数化路线通常在单个容器中具有多个深度。 (2认同)