In responsejs, is it ok to execute async functions in the DidMount component and do setstate as a result of the callback?

I understand that the correct place for initial ajax data loading is in the DidMount component.

It seems to me that I want to make "setState" the result of several asynchronous functions that I will execute.

Thus, I need to put setState in asynchronous callbacks.

Am I doing it right or wrong? Will this cause setState to execute from asynchronous callbacks?

+4
source share
2 answers

, . , , , Ajax , - ; , , , . . isMounted .

+1

async componentDidMount :

async componentDidMount() {
  const res = await fetch('https://example.com')
  const something = await res.json()
  this.setState({something})
}
0

Source: https://habr.com/ru/post/1628611/


All Articles