hook for componentDidMount code example
Example 1: componentdidmount hooks
For componentDidMount
useEffect(() => {
}, []);
For componentDidUpdate
useEffect(() => {
}, [yourDependency]);
For componentWillUnmount
useEffect(() => {
return () => {
}
}, [yourDependency]);
Example 2: react hooks componentdidmount
useEffect(() => {
}, []);
Example 3: useeffect componentdidmount
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
useEffect(() => {
document.title = `You clicked ${count} times`;
});
);
}
Example 4: componentwillunmount hooks
useEffect(() => {
window.addEventListener('mousemove', () => {});
return () => {
window.removeEventListener('mousemove', () => {})
}
}, [])
Example 5: useeffect
import React, { useState, useEffect } from 'react';
function Example() {
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
Example 6: componentdidmount in hooks
useEffect(() => {
}, []);