Javascript ReactJS:React路由器将值传递到另一页
我需要使用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> </
<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()
}
}