React component not re-rendering on state change

后端 未结 7 817
抹茶落季
抹茶落季 2021-02-02 05:59

I have a React Class that\'s going to an API to get content. I\'ve confirmed the data is coming back, but it\'s not re-rendering:

var DealsList = React.createCla         


        
相关标签:
7条回答
  • 2021-02-02 07:00

    I was going through same issue in React-Native where API response & reject weren't updating states

    apiCall().then(function(resp) { this.setState({data: resp}) // wasn't updating }

    I solved the problem by changing function with the arrow function

    apiCall().then((resp) => {
        this.setState({data: resp}) // rendering the view as expected
    }
    

    For me, it was a binding issue. Using arrow functions solved it because arrow function doesn't create its's own this, its always bounded to its outer context where it comes from

    0 讨论(0)
提交回复
热议问题