React project best practices and code snippets
React Project Best Practices Code Snippets Functional components are far more efficient than class based components. There is also less code that is needed to be written to achieve the same goal. Execute code on mount and unmount in functional components Answer is useEffect hook . If we pass an empty array as the second argument, it tells useEffect to fire on component load. This is the only time it will fire. import React, { useEffect } from 'react' ; const ComponentExample = () => { useEffect(() => { // Anything here is fired on component mount }, []); }; If you add a return function inside the useEffect function, it is triggered when a component unmounts from the DOM. const ComponentExample2 = () => { useEffect(() => () => { // Anything here is fired on component unmount }, []); }; You can use componentDidMount, and componentWillUn...