Answers for "how to add componentdidmount to react function"

8

how to use componentdidmount in functional component

// passing an empty array as second argument triggers the callback in useEffect
// only after the initial render thus replicating `componentDidMount` lifecycle behaviour
useEffect(() => {
  if(!props.fetched) {
 	 props.fetchRules();
  }
  console.log('mount it!');
}, []);

// componentDidUpdate
useEffect({
	your code here
}) 

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

// For componentWillUnmount
useEffect(() => {
  // componentWillUnmount
  return () => {
     // Your code here
  }
}, [yourDependency]);
Posted by: Guest on June-08-2020
1

componentdidmount in hooks

useEffect(() => {
  // Your code here
}, []);
Posted by: Guest on December-15-2020

Code answers related to "how to add componentdidmount to react function"

Code answers related to "Javascript"

Browse Popular Code Answers by Language