237

I gather that the useEffect Hook is run after every render, if provided with an empty dependency array:

useEffect(() => {
  performSideEffect();
}, []);

But what's the difference between that, and the following?

useEffect(() => {
  performSideEffect();
});

Notice the lack of [] at the end. The linter plugin doesn't throw a warning.

6 Answers 6

431

It's not quite the same.

  • Giving it an empty array acts like componentDidMount as in, it only runs once.

  • Giving it no second argument acts as both componentDidMount and componentDidUpdate, as in it runs first on mount and then on every re-render.

  • Giving it an array as second argument with any value inside, eg , [variable1] will only execute the code inside your useEffect hook ONCE on mount, as well as whenever that particular variable (variable1) changes.

You can read more about the second argument as well as more on how hooks actually work on the official docs at https://reactjs.org/docs/hooks-effect.html

Sign up to request clarification or add additional context in comments.

8 Comments

Is there a use-case for ever putting null? Isn't it just the same as not putting the code in a useEffect hook?
@Patrick useEffect will be run after render while just putting the code there will be run before render
according to a note in: reactjs.org/docs/…, passing empty array will cause run it also on componentWillUnmount in addition to componentDidMount
When I give the dependency array a variable (Ex: [myList]) to display my list items after I insert them with axios.post and I see that it keeps sending requests every second in my Network tab. Will this affect the performance of my app?
well, yes. you're basically running an infinite loop
|
26

Just an addition to @bamtheboozle's answer.

If you return a clean up function from your useEffect

useEffect(() => {
  performSideEffect();
  return cleanUpFunction;
}, []);

It will run before every useEffect code run, to clean up for the previous useEffect run. (Except the very first useEffect run)

1 Comment

You forgot to mention that the cleanup function will also always run on unmount. So, for example, if the dependency array is empty ([]), then the cleanup function will only run once: on unmount. See "Notes" section here (scroll down).
6

Late to the party but thought of putting this example here which I did for my own understanding after reading above comments:

import './App.css';
import { useEffect, useState } from 'react';

function App() {

  const [name, setName] = useState('John');
   useEffect(()=>{
    console.log("1- No dependency array at all");
  });
  useEffect(()=>{
    console.log("2- Empty dependency array");
  }, []);
  useEffect(()=>{
    console.log("3- Dependency array with state");
  }, [name]);

  const clickHandler = () => {
    setName('Jane');
  }
  return (
    <div className="App">
      <button onClick={clickHandler}>Click to update state</button>
      <p>{`Name: ${name}`}</p>
    </div>
  );
}

export default App;

OUTPUT

On page load

 1- No dependency array at all
 2- Empty dependency array
 3- Dependency array with state
 1- No dependency array at all
 2- Empty dependency array
 3- Dependency array with state

On button click -state update

 1- No dependency array at all
 3- Dependency array with state

1 Comment

Everywhere I see says that an empty array will run only once. I'm trying to use this to bind an event handler. but with empty array it don't run at all.
3

The latest docs have a good rundown on the differences:

https://react.dev/reference/react/useEffect#examples-dependencies

Passing an array

If you specify the dependencies, your Effect runs after the initial render and after re-renders with changed dependencies.

useEffect(() => {}, [a, b]);

Passing an empty array

If your Effect truly doesn’t use any reactive values, it will only run after the initial render (though twice in development). This now causes a linter warning.

useEffect(() => {}, []);

Passing no array

If you pass no dependency array at all, your Effect runs after every single render (and re-render) of your component.

useEffect(() => {});

2 Comments

This is misleading answer, descriptions for empty array and no array should be switched.
If you check the docs, this is what they say. See: react.dev/reference/react/useEffect#examples-dependencies
1

The difference is that if you don't provide the empty array dependency, the useEffect() hook will be executed on both mount and update.

Comments

1
  • React will run the logic inside useEffect, which has an empty dependency array, only when the component is initially rendered.

  • React will run the logic inside useEffect, which has no dependency
    array when the component is initially rendered AND every time its
    props or state change.

  • React will run the logic inside useEffect, which has a dependency
    array with some items, when the component is initially rendered AND
    every time one of these items changes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.