0

Okay, so this issue is draining me guys.

I am sending a request in use effect and it updates the projects state based on the server response. Now, what I want is that when I am done setting the projects state, I wanna set another state called ids that are basically project ids. But ids return an empty array.

My end goal is to make a second request in another use effect using these ids.

I tried doing this but it causes an infinite loop.

useEffect(() => {
const updateProjects = async () => {
  const res = await axios.get(`${baseURL}/projects`, {
    params: {
      email: email,
    },
  });
  setProjects(res.data.projects);
  for (let i = 0; i < projects.length; i++) {
    setIds((ids) => [...ids, projects[i]._id]);
  }
};
updateProjects();

 }, [projects]);

This is my final use effect where I try using the ids:

 useEffect(() => {
console.log(ids);

const getBugs = async () => {
  try {
    await axios
      .get(`${baseURL}/bugs`, {
        params: {
          ids: ids,
        },
      })
      .then((response) => {
        console.log(response.data);
      });
  } catch (err) {
    console.log(err);
  }
};
getBugs();


 }, [ids]);

Your help is highly appreciated!

Undisclosed
  • 15
  • 1
  • 6

0 Answers0