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>
<Route path={`your-path/:id`} component={YourComponent} />
</Switch>
...
Then use hook useParams
to get the parameter
function YourComponent() {
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
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()
}
}
Post a Comment for "ReactJS: React-Router Pass Value To Another Page"