防止嵌套的<link />组件继承先前的<link />的路径(反应路由器4)

时间:2019-02-21 09:15:51

标签: javascript reactjs ecmascript-6 react-router-v4

我有一个Dasboard组件,其中有React-router 链接到

<Link/>个组件
http:<procjethos>/team

<Team/>组件内部,我有一个<Link/>组件应该链接到

http:<projecthost>/list/:listId

但是,它继承自先前的链接,我得到

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>

如何在带有嵌套LINK的React-Router 4中实现所需的行为?

1 个答案:

答案 0 :(得分:1)

在链接路径之前添加/

 <Link to={`/list/${list._id}`} replace>
       <CardItem name={list.name} />
 </Link>