React navigation didfocus event listener works differently between class component and functional component

前端 未结 1 638
醉酒成梦
醉酒成梦 2021-02-19 09:47

When I transition to this screen, it will do some API calls to fetch the latest data. But it seems not trigger the didFocus event to fire the api calls when I transition from an

相关标签:
1条回答
  • 2021-02-19 10:45

    I guess I found the root cause of the inconsistent behavior. There is another hook called useLayoutEffect

    useLayoutEffect The signature is identical to useEffect, but it fires synchronously after all DOM mutations. Use this to read layout from the DOM and synchronously re-render. Updates scheduled inside useLayoutEffect will be flushed synchronously, before the browser has a chance to paint.

    the useLayoutEffect will block the painting while the useEffect will not. That confirm and explains my guess that the didFocus event had fired, but it didn't trigger the listener since it miss the timing

    so in my case, I have to use useLayoutEffect instead of useEffect

    reference: https://kentcdodds.com/blog/useeffect-vs-uselayouteffect https://reactjs.org/docs/hooks-reference.html#uselayouteffect

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