M
M
MFT2020-04-14 18:24:33
React
MFT, 2020-04-14 18:24:33

React: How to output data received via ajax request on page load?

Hello! Can you please tell me how to display the data received via ajax request on page load?
Those. how to make the component render only after receiving data via ajax?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hzzzzl, 2020-04-14
@MFT

state = {loaded: false, data: null}

async componentDidMount() {
  const data = await /* через ajax-запрос */
  this.setState({ loaded: true, data: data })
}

render() {
  if (!this.state.loaded) return null

  // вывести данные
}

D
Dimonchik, 2020-04-14
@dimonchik2013

https://ru.reactjs.org/docs/faq-ajax.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question