component unmount react hooks code example
Example 1: react use effect
useEffect(() => {
},[]);
Example 2: useeffect with cleanup
useEffect(() => {
return () => {
};
},[]);
Example 3: componentdidupdate in hooks
const App = props => {
const didMountRef = useRef(false)
useEffect(() => {
if (didMountRef.current) {
doStuff()
} else didMountRef.current = true
}
}
Example 4: react useEffect
import React, { useEffect } from 'react';
export const App: React.FC = () => {
useEffect(() => {
}, [])
return (
<div>Use Effect!</div>
);
}
Example 5: useeffect componentdidmount
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `You clicked ${count} times`;
});
);
}
Example 6: component unmount hooks
useEffect(() => {
window.addEventListener('mousemove', () => {});
return () => {
window.removeEventListener('mousemove', () => {})
}
}, [])