How to stop memory leak in useEffect hook react

后端 未结 3 1766
孤城傲影
孤城傲影 2021-02-04 05:46

I am using Effect hook to fetch the datas from server and these data are passed to the react table there i have used the same api call to load the next set of datas from server.

3条回答
  •  青春惊慌失措
    2021-02-04 06:04

    The other answers work of course, I just wanted to share a solution I came up with. I built this hook that works just like React's useState, but will only setState if the component is mounted. I find it more elegant because you don't have to mess arround with an isMounted variable in your component !

    Installation :

    npm install use-state-if-mounted
    

    Usage :

    const [count, setCount] = useStateIfMounted(0);
    

    You can find more advanced documentation on the npm page of the hook.

提交回复
热议问题