Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 用道具反应重定向到另一个页面_Reactjs - Fatal编程技术网

Reactjs 用道具反应重定向到另一个页面

Reactjs 用道具反应重定向到另一个页面,reactjs,Reactjs,我有一个具有唯一行id的表。当我单击一行时,我使用该id重定向到一个视图。为此,我做了一个简单的window.location.href=“url/view/”+id在我的查看页面上,我用id=this.props.match.params.id获取url中设置的id 然而,我的视图有使用路由加载不同组件的子菜单,在这样做时,由于明显的原因,当我导航到其他组件时,id会丢失。我需要一种方法,当我单击表行时,将该id作为道具发送 这是我的第一个React应用程序,我正在努力。感谢您的帮助 我的表(

我有一个具有唯一行id的表。当我单击一行时,我使用该id重定向到一个视图。为此,我做了一个简单的
window.location.href=“url/view/”+id
在我的查看页面上,我用
id=this.props.match.params.id获取url中设置的id
然而,我的视图有使用路由加载不同组件的子菜单,在这样做时,由于明显的原因,当我导航到其他组件时,id会丢失。我需要一种方法,当我单击表行时,将该id作为道具发送

这是我的第一个React应用程序,我正在努力。感谢您的帮助

我的表(引导表)

在我的查看页面上

render() {
const id = this.props.match.params.id;
return (
  <Container className="col-md-12">
    <Sidebar part={id} />
        <Router>
          <Switch>
            <Route path="/url/view/:id" exact component={View} />
            <Route path="/url/create" exact component={Add} />
            <Route
              path="/url/test"
              exact
              render={props => <Test {...props} test={id} />}
            />
          </Switch>
        </Router>
  </Container>
);
render(){
const id=this.props.match.params.id;
返回(
}
/>
);

我希望这能说明问题。在我看来,我有一个侧边栏和路由器,它们根据我在侧边栏上单击的内容加载不同的组件。我不确定这是否是最好的方法。但是,当我单击侧边栏上的一个选项时,例如
url/test
,我会传递id以获得一些结果。现在,在侧边栏中,如果我再次单击
url/view/:id
的导航,url中的id将消失,因此不会返回视图。因此,我需要将id作为表格单击中的道具发送,这样它就不会消失。

您可以发布您当前拥有的代码吗?@brycelikes上面列出的狗您可以提供完整的相关代码,包括
子菜单中的处理
组件吗?因此一个解决方案是将id存储在react状态,然后将其传递给我需要它。这样,当url更改时,它就不会丢失。@Bryce喜欢狗我试过了,但它不能满足我的需要
const rowevent = {
  onClick: (e, row) => {
    console.log(row.id);
    window.location.href = "url/view/" + row.id;
  }
};
render() {
const id = this.props.match.params.id;
return (
  <Container className="col-md-12">
    <Sidebar part={id} />
        <Router>
          <Switch>
            <Route path="/url/view/:id" exact component={View} />
            <Route path="/url/create" exact component={Add} />
            <Route
              path="/url/test"
              exact
              render={props => <Test {...props} test={id} />}
            />
          </Switch>
        </Router>
  </Container>
);