防止嵌套的 <Link/> 组件继承先前 <Link/> 的路径(React 路由器 4)
Prevent nested <Link/> component from inheriting path from previous <Link/> (React router 4)
我有一个 Dasboard 组件,其中有 React-router
<Link/>
个正在 link 到
的组件
http:<procjethos>/team
在 <Team/>
组件中我有一个 <Link/>
组件应该 link 到
http:<projecthost>/list/:listId
但是,它继承了以前的 link,我得到
http<projecthost>/team/list/:listId
// Dashboard.jsx
render() {
const { shoppingLists, teams } = this.props;
return (
<Fragment>
<Toolbar isHomePage/>
<div className="wrapper">
<div className="dashboard">
<h2 className="dashboard__heading">
<TeamIcon />
Cohorts
</h2>
<ul className="dashboard__list">
{_map(teams, team => (
<li className="dashboard__list-item" key={team._id}>
<Link to={`team/${team._id}`}>
<CardItem name={team.name} />
</Link>
</li>
))}
</ul>
</div>
</div>
</Fragment>
);
// Team.jsx
<Fragment>
<Toolbar />
<div>
Cohort of id:
{id}
<ul>
{_map(lists, list => (
<li className="dashboard__list-item" key={list._id}>
<Link to={`list/${list._id}`} replace>
<CardItem name={list.name} />
</Link>
</li>
))}
</ul>
</div>
</Fragment>
如何使用嵌套链接在 React-Router 4 中实现所需的行为?
在link路径前加一个/
<Link to={`/list/${list._id}`} replace>
<CardItem name={list.name} />
</Link>
我有一个 Dasboard 组件,其中有 React-router
<Link/>
个正在 link 到
http:<procjethos>/team
在 <Team/>
组件中我有一个 <Link/>
组件应该 link 到
http:<projecthost>/list/:listId
但是,它继承了以前的 link,我得到
http<projecthost>/team/list/:listId
// Dashboard.jsx
render() {
const { shoppingLists, teams } = this.props;
return (
<Fragment>
<Toolbar isHomePage/>
<div className="wrapper">
<div className="dashboard">
<h2 className="dashboard__heading">
<TeamIcon />
Cohorts
</h2>
<ul className="dashboard__list">
{_map(teams, team => (
<li className="dashboard__list-item" key={team._id}>
<Link to={`team/${team._id}`}>
<CardItem name={team.name} />
</Link>
</li>
))}
</ul>
</div>
</div>
</Fragment>
);
// Team.jsx
<Fragment>
<Toolbar />
<div>
Cohort of id:
{id}
<ul>
{_map(lists, list => (
<li className="dashboard__list-item" key={list._id}>
<Link to={`list/${list._id}`} replace>
<CardItem name={list.name} />
</Link>
</li>
))}
</ul>
</div>
</Fragment>
如何使用嵌套链接在 React-Router 4 中实现所需的行为?
在link路径前加一个/
<Link to={`/list/${list._id}`} replace>
<CardItem name={list.name} />
</Link>