Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call loading function with React useEffect only once

The useEffect React hook will run the passed in function on every change. This can be optimized to let it call only when the desired properties change.

What if I want to call an initialization function from componentDidMount and not call it again on changes? Let's say I want to load an entity, but the loading function doesn't need any data from the component. How can we make this using the useEffect hook?

class MyComponent extends React.PureComponent {     componentDidMount() {         loadDataOnlyOnce();     }     render() { ... } } 

With hooks this could look like this:

function MyComponent() {     useEffect(() => {         loadDataOnlyOnce(); // this will fire on every change :(     }, [...???]);     return (...); } 
like image 903
Dávid Molnár Avatar asked Nov 02 '18 14:11

Dávid Molnár


People also ask

How do you call a function once in useEffect?

Here is an example of how you would call a function to fetch data only once - when the component mounts. Copied! import {useEffect, useState} from 'react'; const App = () => { const [data, setData] = useState({data: []}); const [err, setErr] = useState(''); useEffect(() => { // 👇️ this only runs once console.

How do you run useEffect only once in react?

Side Effect Runs Only Once After Initial Render You do not want to make this API call again. You can pass an empty array as the second argument to the useEffect hook to tackle this use case. useEffect(() => { // Side Effect }, []); In this case, the side effect runs only once after the initial render of the component.

Why does useEffect get called twice?

If you have just made a new project using Create React App or updated to React version 18, you will notice that the useEffect hook is called twice in development mode. This is the case whether you used Create React App or upgraded to React version 18.

Why is useEffect running multiple times?

If your application is behaving strangely after updating to React 18, the default behavior of useEffect changed to run it 2 times.


2 Answers

If you only want to run the function given to useEffect after the initial render, you can give it an empty array as second argument.

function MyComponent() {   useEffect(() => {     loadDataOnlyOnce();   }, []);    return <div> {/* ... */} </div>; } 
like image 179
Tholle Avatar answered Oct 08 '22 03:10

Tholle


TL;DR

useEffect(yourCallback, []) - will trigger the callback only after the first render.

Detailed explanation

useEffect runs by default after every render of the component (thus causing an effect).

When placing useEffect in your component you tell React you want to run the callback as an effect. React will run the effect after rendering and after performing the DOM updates.

If you pass only a callback - the callback will run after each render.

If passing a second argument (array), React will run the callback after the first render and every time one of the elements in the array is changed. for example when placing useEffect(() => console.log('hello'), [someVar, someOtherVar]) - the callback will run after the first render and after any render that one of someVar or someOtherVar are changed.

By passing the second argument an empty array, React will compare after each render the array and will see nothing was changed, thus calling the callback only after the first render.

like image 43
Edan Chetrit Avatar answered Oct 08 '22 05:10

Edan Chetrit