React component not re-rendering on state change

后端 未结 7 802
抹茶落季
抹茶落季 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 06:43

    I'd like to add to this the enormously simple, but oh so easily made mistake of writing:

    this.state.something = 'changed';
    

    ... and then not understanding why it's not rendering and Googling and coming on this page, only to realize that you should have written:

    this.setState({something: 'changed'});
    

    React only triggers a re-render if you use setState to update the state.

提交回复
热议问题