0

I am noticing that my component is stuck in an infinite loop. It keeps making the same rest call.

It is my understanding that useEffect hooks work similar to ComponentRender and ComponentUpdate lifecycle functions.

Also, I think what is happening is this but not able to fix it:

On render - > API called -> Updated Error state -> Component Updated -> UseEffect Hooked called again

Can you help me?

import React, { useState, useEffect } from "react";
import "./ResultsPage.css";
import Error from "../components/Error";

function Homepage() {
  let username = "test";

  const [error, setError] = useState({ exists: false, value: null });
  const [userData, setUserData] = useState(null);

  const getUsernameData = () => {
    fetch(`testAPI/${username}`)
      .then((response) => {
        if (response.status === 404) {
          return setError({ exists: true, value: response.status });
        }
        return response.json();
      })
      .then((json) => setUserData(json))
      .catch((error) => {
        setError({ active: true, type: 400 });
      });
  };
  useEffect(() => {
    getUsernameData();
  });

  return (
    <div className="Homepage">
      {error.exists && <Error error={error} username={username} />}
      {!error.exists && <h1>Error does not exist</h1>}
    </div>
  );
}

export default Homepage;
0

2 Answers 2

2

useEffect will run on every render unless you tell it otherwise. Your effect has no dependency array, so it is running every render, triggering a re-render, and then starting over again infinitely.

You can tell the useEffect to only run once on mount by giving it an empty array:

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

You can also tell it to only run when certain things change. Do this by adding the variables to the dependency array:

useEffect(() => {
  getUsernameData();
}, [variable_to_depend_on]);
Sign up to request clarification or add additional context in comments.

2 Comments

upvoting Brian's answer for including the 'why' as well as the the 'what'.
Thanks @Brian for taking your time to answer this in-depth!
1

Try changing...

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

to

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

2 Comments

Thank you. For anyone wanting to know how it is done, follow this link : reactjs.org/docs/…
You should a) memoize your fetch function with useCallback and b) add it to your dependency array for useEffect. Or if not, you may wish to explain why....

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.