Why useState makes the component to render twice?

前端 未结 1 382
眼角桃花
眼角桃花 2021-01-27 02:45

maybe some of you can open my eyes.

I don\'t understand why in this code: https://codesandbox.io/s/use-state-renders-twice-6r1xl component App renders twice when mounte

相关标签:
1条回答
  • 2021-01-27 03:17

    It is because of React.StrictMode and this only happens in development. If you remove React.StrictMode you will get only 1 log.

    For more details, check this thread on react repo:

    https://github.com/facebook/react/issues/15074

    Also check React docs: https://reactjs.org/docs/strict-mode.html#detecting-unexpected-side-effects

    Check the app without StrictMode here: https://codesandbox.io/s/use-state-renders-twice-3vroc

    Hope this helps!

    0 讨论(0)
提交回复
热议问题