UseEffect being called multiple times
Asked Answered
B

6

59

I thought useEffect is called once only after render, but it's being executed multiple times and not in the order I expected.

I expected it to msg 'data loading' while the fetch is in progress and then once fetch is done, render the title field and alert "..Done..." once and that should be the end.

I added ALERT and console logs at the two points to determine the flow and both alerts and console logs appear more than once and in different orders. Could you kindly run this code and see the behaviour. I kept the 2nd argument array as null to make it run once only but does not help.

Please clarify if react RENDER means DISPLAY on screen? what stage does LOAD indicate? When is the display done?

code follows:

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

function DemoFetchZ() {
  let data = { title: "Waiting for Data" };
  const [todo, setTodo] = useState(data);
  const [isData, setData] = useState(false);
  const [isFetching, setFetching] = useState(false);

  useEffect(() => { // called after the first render
    async function fetchData() {
      setFetching(true);
      const response = await fetch(
        "https://jsonplaceholder.typicode.com/todos/1"
      );
      console.log("response = ", response);
      let data = await response.json();
      setTodo(data); //updt state
        setFetching(false);
        setData(true)
      console.log("Data = ", data);
    }
    fetchData();
  }, []); //[isData] null value will execute once only?

  if (isFetching) {
      console.log("data loading ......")
      alert ("data loading")
      return (<div>...Data Loading.....</div>);
  }

  return (
    <div>
           - Fetch
          <br /> {alert("..DONE...")}
      <span>Title: {todo.title}</span>
    </div>
  );
}

export default DemoFetchZ;
Bacteria answered 29/6, 2020 at 4:6 Comment(0)
S
79

Your useEffect is executed only once per render cycle, but you have several state updates in your useEffect which cause a re-render. Hence you get a lot of alerts.

See a demo of your code and see the console.logs as well as comments

Also note that useEffect will

  • when you provide empty array dependency, your useEffect execute once
  • when you provide some value as dependency (eg: [name] ), your useEffect execute when name state/prop changes
  • useEffect executes on every re-render if you don't pass the dependency array.

Read here on re-render

Synergism answered 29/6, 2020 at 4:30 Comment(1)
R
36

For future readers.

React 18 introduces a new development-only check to Strict Mode. This new check will automatically unmount and remount every component, whenever a component mounts for the first time, restoring the previous state on the second mount.

The useEffect callback in this case runs twice for the initial render. After state change, the component renders twice, but the effect should run once.

Read the Offical React Doc for more troubleshooting

Read this link, or this for more details.

Runofthemill answered 16/8, 2022 at 6:30 Comment(0)
S
25

refactor it like this.

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

const DemoFetchZ = () => {
  const [todo, setTodo] = useState({});
  const [loading, setLoading] = useState(false);

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

  const fetchData = () => {
    setLoading(true);
    fetch("https://jsonplaceholder.typicode.com/todos/1")
      .then((response) => response.json())
      .then((data) => {
        setTodo(data);
        setLoading(false);
      })
      .catch((error) => {
        console.log(error);
        setLoading(false);
      });
  };

  return (
    <>
      {loading ? (
        <div>...Data Loading.....</div>
      ) : (
        <div>
          - Fetch
          <br />
          <span>Title: {todo ? todo.title : "no Title Found"}</span>
        </div>
      )}
    </>
  );
};

export default DemoFetchZ;
Stiegler answered 29/6, 2020 at 7:3 Comment(6)
That looks much better. I am going to try it. One question though. Why not have function fetchData as asynch-await?? const fetchData = () => { setLoading(true); fetch("jsonplaceholder.typicode.com/todos/1") .then((response) => response.json()) ..Bacteria
.then is working just like async-await and it's better for DX. in the above code you told the compiler just set data when the response is 200 otherwise set error. but in your code you don't handle the errors.Stiegler
@SerajVahdati I am learning react.js and have faced somewhat a similar problem. What I understand from this answer is that the state objects like todo and loading should not be updated inside the useEffect method if it is passed as a dependency, otherwise useEffect will be called again and again. In my case, I am fetching data from an API and displaying it on the same page. By applying code fix as mentioned in this answer, I have a warning: "React Hook useEffect has a missing dependency: 'fetchData'. Either include it or remove the dependency array."Tin
@Tin it's just a warn and it's not important. nothing is wrong with this code. but if you want to fix it. you can declare your fetchData function inside the useEffect and call it right after the declaration. it's had to be fixed for youStiegler
One small improvement. If you end up putting dependencies in useEffect, putting a check to see if you already have your data in fetchData() and return instead of calling the server again will stop you from spamming the server with the same request when not needed. fetchData(){ if ( data ) { return; } setLoading(true);....}Emie
please check my post :https://mcmap.net/q/64783/-why-useeffect-running-twice-and-how-to-handle-it-well-in-react/18209107Scrutable
M
24

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. In this article, we'll learn about the useEffect hook in React 18's Strict Mode, which has a strange behavior. The standard behavior of the useEffect hook was modified when React 18 was introduced in March of 2022. If your application is acting weird after you updated to React 18, this is simply due to the fact that the original behavior of the useEffect hook was changed to execute the effect twice instead of once.

If we used the useEffect hook as follows:

useEffect(() => {
  console.log("First call on mount..");

  return () => console.log("Cleanup..");
}, []);

The output to the console should look like this:

First call on mount..
Cleanup..
First call on mount..

The useEffect hook, which should only be called on the first mount, is called two times.

Now what if we need to use the useEffect hook to fetch data, so that it does not fetch twice? One easy solution to this behavior is to disable strict mode. Open the src/index.js file and remove the StrictMode higher order component:

import React, { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';

import App from './App';

const rootElement = document.getElementById('root');
const root = createRoot(rootElement);

root.render(
  <StrictMode>
    <App />
  </StrictMode>
);
Moneyer answered 21/10, 2022 at 19:49 Comment(3)
What are the consequences of disabling StrictMode?Tiffaneytiffani
React's Strict Mode is a development mode only feature for highlighting potential problems in an application. Like Fragment, StrictMode does not render any visible UI. It activates additional checks and warnings for its descendants. StrictMode currently helps with: 1-Identifying components with unsafe lifecycles. 2-Warning about legacy string ref API usage. 3-Warning about deprecated findDOMNode usage. 4-Detecting unexpected side effects. 5-Detecting legacy context API. 6-Ensuring reusable state.Moneyer
this method does not working for meForegut
B
5

Here is the solution We can prevent multiple rendering on useEffect using useRef hook

const initialized = useRef(false);
useEffect(() => {
  if (!initialized.current) {
    initialized.current = true

    // Your code on effect here...
    ...
  }
}, []) // You may set dependency also
Buddybuderus answered 12/8, 2023 at 4:50 Comment(2)
You are managing the symptom rather then solving the problem. This is a hack/patch not a solution that solves the actual problem.Fated
Is there another way to go about it w/o having multiple rerenders outside of the initial 2 rerenders?Agonistic
C
0

I faced this issue even though I had turned off reactStrictMode mode while using React 16 and NextJS 14.2.2 but then I figured it was a simple missing parameters. I'm not sure if this is an intended behaviour. I was calling useEffect without any parameters passed in like:

useEffect(() => {
  if(someCondition(someExpression)){ router.push("/path/to/render"); }
  else{ router.push("/") }
});

And changed this to:

useEffect(() => {
  if(someCondition(someExpression)){ router.push("/path/to/render"); }
  else{ router.push("/") }
},[]);

Notice the only change is passing the second parameter([]) to the useEffect call. Alternatively, this also solved the NextJS's issue: Abort fetching component for route: "/login"

Hope this saves somebody's time

Chandlery answered 29/4, 2024 at 11:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.