50

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;
VLAZ
  • 26,331
  • 9
  • 49
  • 67
nktoronto
  • 711
  • 1
  • 7
  • 10

5 Answers5

74

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 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

User
  • 31,811
  • 40
  • 131
  • 232
gdh
  • 13,114
  • 2
  • 16
  • 28
  • please check my post .tnx :https://stackoverflow.com/q/74798936/18209107 –  Dec 15 '22 at 11:49
26

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.

Bino
  • 744
  • 15
  • 22
21

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;
Seraj Vahdati
  • 439
  • 3
  • 9
  • 1
    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("https://jsonplaceholder.typicode.com/todos/1") .then((response) => response.json()) .. – nktoronto Jun 29 '20 at 22:13
  • .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. – Seraj Vahdati Jun 30 '20 at 07:04
  • @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." – Junaid Oct 09 '21 at 16:43
  • @Junaid 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 you – Seraj Vahdati Oct 27 '21 at 16:19
  • 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);....}` – Dev Null Nov 29 '21 at 07:24
  • please check my post :https://stackoverflow.com/q/74798936/18209107 –  Dec 15 '22 at 11:49
19

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>
);
Omar Saade
  • 320
  • 2
  • 8
  • 3
    What are the consequences of disabling StrictMode? – yngrdyn Nov 29 '22 at 10:55
  • 1
    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. – Omar Saade Dec 01 '22 at 19:54
  • 1
    this method does not working for me – Fai Zal Dong Mar 15 '23 at 04:54
0

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
Kaushal Sachan
  • 1,175
  • 11
  • 8