Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.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
Javascript ReactJS:React路由器将值传递到另一页_Javascript_Reactjs_React Router_React Router Dom - Fatal编程技术网

Javascript ReactJS:React路由器将值传递到另一页

Javascript ReactJS:React路由器将值传递到另一页,javascript,reactjs,react-router,react-router-dom,Javascript,Reactjs,React Router,React Router Dom,我需要使用react路由器将一个值从一个页面传递到另一个页面 我试过这样做: <li> <A className="btn btn-link btn-sm" href={{ pathname: Routes.detail_page, search: `?_id=${C.Code}`, state: this.props.current }}> { 'Open' } </A> </

我需要使用react路由器将一个值从一个页面传递到另一个页面

我试过这样做:

<li>
    <A 
      className="btn btn-link btn-sm"
      href={{ pathname: Routes.detail_page, search: `?_id=${C.Code}`, state: this.props.current  }}>
      { 'Open' }
    </A>
</li>

怎么办?

您必须确保在
路由中定义了URL参数

// file-where-you-define-routes.js
...
<Switch>
  <Route path={`your-path/:id`} component={YourComponent} />
</Switch>
...
或者如果您使用类组件
this.props.match.params.id

class YourComponent extends Component {
  ...
  componentDidMount(){
    let C = this.props.match.params.id; // match
    console.log("C is: ", C) // this should be defined now
    this.updateIsDeletableState()
  }
}

您可以添加“其他页面代码”吗?我只尝试在componentDidMount()中打印
this.props.current
usign console.log。你需要知道什么?:)我想看看你是如何编写代码的。我编辑了这条消息,确保你在试图访问道具的组件中有以下内容:``构造函数(道具){super(道具);this.state={//states};}```此外,路由can声明也可以更新如下以通过道具
}/>
function YourComponent() {
  const { id } = useParams();
  let C = id;
  console.log("C is: ", C) // this should be defined now
  ...
}
class YourComponent extends Component {
  ...
  componentDidMount(){
    let C = this.props.match.params.id; // match
    console.log("C is: ", C) // this should be defined now
    this.updateIsDeletableState()
  }
}