score:0

Accepted answer
 useeffect(() => {
const config = {
  headers: {
    authorization: 'bearer ' + localstorage.getitem('token')
  }
}
axios.get('users', config).then((res) => {
  if (res.status == 200 && res.data.msg == "success") {
    setusers(res.data.users)
  }
  else {
    history.push("/");
  }

  feather.replace()
  });
}, []); 

note here i passed the empty array[] as the second argument for useeffect,

reason of empty array at useeffect

if you want to run an effect and clean it up only once (on the mount and unmount), you can pass an empty array ([]) as a second argument. this tells react that your effect doesn’t depend on any values from props or state, so it never needs to be re-run. this isn’t handled as a special case — it follows directly from how the dependencies array always works.

read more about useeffect

score:0

you would want to add an empty array as useeffect dependency, so it only run once:

useeffect(() => {
  const config = {
    headers: {
      authorization: "bearer " + localstorage.getitem("token"),
    },
  };
  axios.get("users", config).then((res) => {
    if (res.status == 200 && res.data.msg == "success") {
      setusers(res.data.users);
    } else {
      history.push("/");
    }

    feather.replace();
  });
}, []);  //<-- add empty dependency here

score:0

the useeffect hook basically accepts two arguments, the first one being the callback, and the second one is an array of dependencies (optional). the purpose of the second argument (array of dependencies) is to trigger the callback function, whenever any value in the dependencies array change. so, if you don't use the dependencies array in your useeffect hook, the callback function is executed every time the component is rendered, i.e, whenever the state variables are mutated.

so what's causing the loop?

const [ users, setusers ] = usestate([]);
  useeffect(() => {
    const config = {
      headers: {
        authorization: 'bearer ' + localstorage.getitem('token')
      }
    }
    axios.get('users', config).then((res) => {
      if (res.status == 200 && res.data.msg == "success") {
        setusers(res.data.users) //updating state causes the component to re-render
      }
      else {
        history.push("/");
      }

      feather.replace()
    });

  });

looking at your code, we can observe that you are using two hooks ( usestate and useeffect) , so when the promise is resolved, you are updating your users state with the latest fetched information. so far, so good. when a state is updated, we know that the component is re-rendered (that's the basic principle on how react works). so updating the users state causes the component function to re-render, which would again invoke useeffect which would again update users state, and so forth.

workaround

to avoid this loop, we'd want to execute useeffect only once, and not every time the component is rendered. this could be accomplished by passing an empty array as the second argument (which basically tells react to run `useeffect` only when the component is rendered for the first time).
const [ users, setusers ] = usestate([]);
  useeffect(() => {
    const config = {
      headers: {
        authorization: 'bearer ' + localstorage.getitem('token')
      }
    }
    axios.get('users', config).then((res) => {
      if (res.status == 200 && res.data.msg == "success") {
        setusers(res.data.users) //updating state causes the component to re-render
      }
      else {
        history.push("/");
      }

      feather.replace()
    });

  },[]); //empty array as the second argument

this is similar to componentdidmount when working with class-based components. further, you could also add variables to the array that would cause the useeffect to be invoked only when the variables in the array are mutated.

score:0

add dependency array to you're useeffect hook , this will solve you're problem

this will act like component did mount

useeffect(()=> {
 /// you're code    
} , []) 

but the one that you used acts like component did update and causes problem because you need to execute the code only when component mounted


Related Query

More Query from same tag