2

In my ReactJS project, I use fetch the do the async processing, and after fetch the data,

I want to setState to change my local state. But I get the error return.

Uncaught (in promise) TypeError: Cannot read property 'setState' of undefined

Fetch function code:

AddDeal(deal){
fetch('shops/1/deals',{
      method: "POST",
      body: JSON.stringify(deal),
      headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
      },
    }).then(response => {
         response.json().then(data =>{
           this.setState({deals: data}); // and use this.props.dispatch I get `props` of undefined 
         })
      })
}

I have watched the other question like mine React.js: loading JSON data with Fetch API and props from object array

So how can I fix it?

5
  • from where you are calling AddDeal method? Commented Aug 26, 2017 at 14:14
  • Do you bind AddDeal? Commented Aug 26, 2017 at 14:18
  • have you tried binding this to AddDeal()? Would be good to include how you execute AddDeal() Commented Aug 26, 2017 at 14:18
  • @MayankShukla My React Component, this method is running when I submit the form Commented Aug 26, 2017 at 14:18
  • @edgaromar90 I haven't bind.this.AddDeal(). Let me try Commented Aug 26, 2017 at 14:19

4 Answers 4

6

When you call fetch(), the value of this will be the response, not the class. You can save the value of this by assigning it to a variable, usually called self.

AddDeal(deal){
  const self = this;
  fetch('shops/1/deals',{
    method: "POST",
    body: JSON.stringify(deal),
    headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
    },
  }).then(response => {
    response.json().then(data =>{
      self.setState({deals: data});
    })
  })
}
Sign up to request clarification or add additional context in comments.

3 Comments

Better declare AddDeal like so : AddDeal = (deal) => {...}
So Is it any association for bind(this) ? I tried to follow the other answer to bind(this) the AddDeal function, but it since did not work.
Two separate issues. For this to work in React, you need to bind the function to the class or use arrow functions. Additionally, fetch overrides this as I explained. this can be quite confusing in JS
1

This will probably solve your issue:

AddDeal = (deal) => {
  fetch('shops/1/deals',{
    method: "POST",
    body: JSON.stringify(deal),
    headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
    },
  }).then(response => {
    response.json().then(data =>{
      this.setState({deals: data});
    })
  })
}

Comments

0

Try like this, this is the right approach

constructor(props) {
    super(props)

   ..............
   ..............
   ..............

    this.AddDeal = this.AddDeal.bind(this)
}

Comments

-2

I would suggest you to use Axios rather than using fetch, because axios has more great features as compared to Fetch. You can read about the great features of Axios from here Link.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.