javascript - React Router Dom 参数无法正常工作?

标签 javascript react-router react-router-dom

这是我的路由配置代码:

<Switch>
   <Route exact path='/(home)?' component={TodoListHomePage} />
   <Route exact path='/profile/:userId' component={TodoListProfilePage} /> 
   <Route  path='/login' component={SignUpAndLogin} /> 
</Switch>

一切正常,但参数路径很奇怪,第一次单击它工作正常,路径例如

http://localhost:4000/Profile/597c1f43a87ca40d38f79a68

第二次单击它会连接个人资料,如下所示:

http://localhost:4000/Profile/Profile/597c1f43a87ca40d38f79a68

例如

http://localhost:4000//Profile/Profile/Profile/Profile/597c1f43a87ca40d38f79a68

这是我在单击个人资料链接时调用的函数:

handleProfileClick = (e, {name}) => {
      this.setState({ activeItem: name }); 
      this.props.history.push(name.concat('/'.concat(this.props.viewer._id.toString())));
}

最佳答案

您正在使用相对路径。只需在连接之前添加一个 / 即可。

handleProfileClick = (e, {name}) => {
      this.setState({ activeItem: name }); 
      this.props.history.push("/profile/" + this.props.viewer._id.toString());
}

关于javascript - React Router Dom 参数无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45658203/

相关文章:

reactjs - react Hook : Component is not rendering in Nested Routing

javascript - 无法使用 Link 组件传递状态。为什么状态未定义?

javascript - 将 JavaScript 接口(interface)与 Rails 后端集成

javascript - 使用 Polymer.Base.importHref 的 polymer 错误 "A type with that name is already registered"

javascript - 确认删除窗口-点击删除或取消时删除记录

reactjs - 初始页面加载时的 react 路由器过渡动画

JavaScript 的 'forEach' 的误解

reactjs - React router v6 socket 似乎不起作用

reactjs - react 路由器 : Route defined in child component not working

javascript - React router v4 无法构建