最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReactJS when Fetch POST, and use then function to setState - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question asked Aug 26, 2017 at 14:12 Champer WuChamper Wu 1,2713 gold badges14 silver badges37 bronze badges 5
  • from where you are calling AddDeal method? – Mayank Shukla Commented Aug 26, 2017 at 14:14
  • Do you bind AddDeal? – Nocebo Commented Aug 26, 2017 at 14:18
  • have you tried binding this to AddDeal()? Would be good to include how you execute AddDeal() – Edgar Henriquez Commented Aug 26, 2017 at 14:18
  • @MayankShukla My React Component, this method is running when I submit the form – Champer Wu Commented Aug 26, 2017 at 14:18
  • @edgaromar90 I haven't bind.this.AddDeal(). Let me try – Champer Wu Commented Aug 26, 2017 at 14:19
Add a ment  | 

4 Answers 4

Reset to default 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});
    })
  })
}

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});
    })
  })
}

Try like this, this is the right approach

constructor(props) {
    super(props)

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

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

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

发布评论

评论列表(0)

  1. 暂无评论