do not trigger useeffect on start

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

The above code snippet is an example of how to use the useEffect hook in React to run a function after a component mounts. By passing in an empty array as a second argument, the function will only run on initial mount and not on subsequent renders - this is a great way to prevent unnecessary triggers. However, sometimes you may need to have the function run on initial mount *and* on subsequent renders. In this case, you can omit the second argument entirely and the function will run on every render. Alternatively, you could also pass in a variable (such as a prop or state) that changes on subsequent renders, causing the useEffect hook to run. It's important to note that, while not passing in the second argument can improve performance, it could also have unintended side effects, especially if your function has side effects. To avoid this, make sure that you test your code thoroughly and consider other alternatives such as componentDidMount or useLayoutEffect.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe