samedi 28 octobre 2017

React-on-Rails: Error when updating state in axios request

I'm debugging a code which was written by someone else. In componentDidMount() i'm sending an axios request and updating the state. render() renders data successfully.

But after clicking next button I need to make another axios request, update the state and render new data. I use componentWillReceiveProps(nextProps) for that. I successfully make axios request, it's returning correct data, but it doesn't want to change state, it throws several errors instead. How can I fix it?

componentWillReceiveProps(nextProps) {
  this.qsParams = queryString.parse(nextProps.location.search)
  this.quoteId = Number(this.qsParams.quote)

  axios.get(`api/quotes/${this.quoteId}`)
  .then(response => {
    console.log("Data: ", response.data); // prints correct data
    this.setState({ //not being invoked, throws errors
      quote: response.data
    }, ()=> {console.log("fetch: ", this.state);})
  })
  .catch(error => {
    console.error(error)
    this.setState({ fireRedirect: true })
  })
}

Aucun commentaire:

Enregistrer un commentaire