Reactjs: React-router Pass Value To Another Page
I have need to pass a value from a page to another using react-router. I have tried in this way:
Solution 1:
You have to make sure that you have defined URL parameter in the Route
// file-where-you-define-routes.js
...
<Switch><Routepath={`your-path/:id`} component={YourComponent} /></Switch>
...
Then use hook useParams
to get the parameter
functionYourComponent() {
const { id } = useParams();
let C = id;
console.log("C is: ", C) // this should be defined now
...
}
OR if you use class component this.props.match.params.id
classYourComponentextendsComponent {
...
componentDidMount(){
let C = this.props.match.params.id; // matchconsole.log("C is: ", C) // this should be defined nowthis.updateIsDeletableState()
}
}
Post a Comment for "Reactjs: React-router Pass Value To Another Page"