search

Home  >  Q&A  >  body text

javascript - ajax at didMount or willMount


After looking at the breakpoints, the order is 16-30-21-30. That is, after willmount, the asynchronous method render and load are executed in the task queue, setState, diff, then render
Why do they say ajax is required in didmount

天蓬老师天蓬老师2708 days ago1128

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-07-05 11:09:35

    Whether an asynchronous request is triggered in willMounnt or didMount, it will be rendered twice. The first time is when the component is loaded, and the second time is when the asynchronous request returns setState.

    However, the logic should be to display the component, such as the loading status, and then trigger the request. Another most important point is that after react fiber is updated, it will trigger willMounnt multiple times, which will cause bugs. Therefore, asynchronous requests are generally placed on didMount.

    reply
    0
  • Cancelreply