синхронно с asyn c await componentDidMount React - PullRequest
0 голосов
/ 08 апреля 2020

У меня проблемы при переходе от синхронного к асинхронному c componentDidMount при React: Фактический синхронный код . Я бы хотел, чтобы вы делали выборки последовательно: сначала get Language, затем getMainPage, а затем getPosts:

 componentDidMount() {


    let currentComponent = this;

     fetch("http://localhost:3001/language")
      .then(function (response) {
        return response.json();
      })
      .then(function (data) {
        const idioma = data.data;
        return  fetch("http://localhost:3001/api/getMainPage?num=" + idioma);
      })
      .then(function (response) {
        return response.json();
      })
      .then(function (response) {
        currentComponent.setState({ response: response.data[0] });
        return  fetch("http://localhost:3001/api/posts/" + response.data[0].ID);
      })
      .then(function (response) {
        return response.json();
      })
      .then(function (response) {
        currentComponent.setState({ posts: response.data });
      })
      .catch(function (error) {
        console.log("Requestfailed", error);
      });

  }

Спасибо!

...