componentDidMount() hooks code example

Example 1: componentdidmount hooks

For componentDidMount
useEffect(() => {
  // Your code here
}, []);

For componentDidUpdate
useEffect(() => {
  // Your code here
}, [yourDependency]);

For componentWillUnmount
useEffect(() => {
  // componentWillUnmount
  return () => {
     // Your code here
  }
}, [yourDependency]);

Example 2: component did mount in hooks

For componentDidMount
useEffect(() => {
  // Your code here
}, []);

For componentDidUpdate
useEffect(() => {
  // Your code here
}, [yourDependency]);

For componentWillUnmount
useEffect(() => {
  // componentWillUnmount
  return () => {
     // Your code here
  }
}, [yourDependency]);