Reputation: 9839
Is there a way to have a hook know when the react has totally finished rendering / mounted?
Not like..
const mountedRef = useRef(false);
useEffect(() => {
if (mountedRef.current) {
return
}
// sync to database
}, [blogtitle])
This just keeps track of times render is called.
Is there way to use hooks to ACTUALLY be equivalent to componentDidMount?
Upvotes: 0
Views: 107
Reputation: 557
Notice the removal of blogtitle
below. This useEffect() is the equivalent of componentDidMount as it will only run on initial render.
const mountedRef = useRef(false);
useEffect(() => {
if (mountedRef.current) {
return
}
// sync to database
}, [])
Upvotes: 1
Reputation: 1039
You can return a function from useEffect. The returned function automatically called by React once the update finishes. You can check this post: https://reactjs.org/docs/hooks-effect.html#effects-with-cleanup
Upvotes: 0