I
I
IIITRIX2018-09-04 07:47:19
React
IIITRIX, 2018-09-04 07:47:19

How to fix Cannot read property 'params' of undefined?

componentDidMount() {
  const postID = this.props.match.params.postID
    axios.get(`http://127.0.0.1:8000/api/${postID}`)
    .then(res => {
      this.setState({
        post: res.data
      });
    })
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2018-09-04
@IIITRIX

You need to figure out the reason for this error.
Its essence is that you have this.props.match - undefined, which means (most likely, since I don’t see the code) - you render the component not from <Router />the component (or not from the withRouter wrapper ).
If there is no desire to deal with the reason, and you just want to make a "patch", then you can do this:

componentDidMount() {
  if (this.props.match && this.props.match.params.postID) {
    const postID = this.props.match.params.postID
      axios.get(`http://127.0.0.1:8000/api/${postID}`)
      .then(res => {
        this.setState({
          post: res.data
        });
      })
    }
// если надо можете сделать else
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question