I am new to react and I try to pass Id from a component to another component. To do that I used
<a className="btn btn-view" href={`/buyer/viewpostdetails/${posts._id}`}>View Post <i className="fas fa-angle-double-right"></i></a>
this code. It works correctly and shows the URL correctly with the ID.
Then I tried to get that Id
componentDidMount(){
const id = this.props.match.params.id;
axios.get(`/post/${id}`).then((res) => {
if (res.data.success) {
this.setState({
post:res.data.post
});
console.log(this.state.post);
}
});
}
I used the above code to do that but I got an error
TypeError: Cannot read property 'params' of undefined
How do I solve this issue?
this.props.match.params.idis? Are you usingreact-router?constructor(props){ super(props); this.state={ post:{} }; } componentDidMount(){ const id = this.props.match.params.id; axios.get(/post/${id}).then((res) => { if (res.data.success) { this.setState({ post:res.data.post }); console.log(this.state.post); } }); }