0

I am trying to wrap my head around the useEffect hook. To my knowledge, if a dependency array is included in the hook, the effect will only run if that particular piece of state changes, however I cannot get it to work. Every time I add a dependency array, it loops infinately.


    useEffect(() => {

      axiosWithAuth()
        .get(`/api/parent/children/${id}`)
        .then(response => {
          // console.log('API response: ', response);
          // console.log('childs data: ',response.data);
          setData(response.data)
          // console.log('new data: ', data);
        });
      .catch(err => console.log(err));
    }, [data]);

if I pull data out of the dependency array, it works fine, no looping. The premise is, if a user clicks on a child's name, it brings up the data for that child (this is a chore-tracker that I'm working on) so I would ASSUME (and I could be wrong) that...

I think I may have answered this on my own, but I would like some confirmation:

I need to set a slice of state for the child, and if THAT state changes, then the effect runs.. so.. something like this..


    const {data, setData} = useState([]);
    const {child, setChild} = useState('');

      useEffect(() => {

        axiosWithAuth()
          .get(`/api/parent/children/${id}`)
          .then(response => {
            // console.log('child list response: ', response);
            // console.log('childs data length',response.data.length);
            // console.log('childs data',response.data);
            setData(response.data)

            // console.log('new data: ', data);
          });
          .catch(err => console.log(err));
      }, [child]);

Is my thinking right in that I need to do it that way instead of the way I had it?

3
  • And yes, I plan on using Global State with either Redux or Context, but I've actually already done it this way, and will just refactor. Commented Feb 9, 2020 at 6:51
  • can you add the definition part of code of "data" and where you change it Commented Feb 9, 2020 at 7:09
  • data is just a slice of state that's being modified by the get. So it pulls the parent data by ID, which contains an array of the children and their data as objects inside the parent's object. Commented Feb 9, 2020 at 17:13

1 Answer 1

1

Yes you are doing fine . But the better answer for these kind of problem is :

useEffect(() => {
        axiosWithAuth()
          .get(`/api/parent/children/${id}`)
          .then(response => {
            // console.log('child list response: ', response);
            // console.log('childs data length',response.data.length);
            // console.log('childs data',response.data);
            setData(response.data)

            // console.log('new data: ', data);
          });
          .catch(err => console.log(err));
} , []})

When you pass an empty array to useEffect it only runs once

Sign up to request clarification or add additional context in comments.

1 Comment

Oh Oh oh.. because the child data is INSIDE the parent's object, I do only need to pull the record once.. (the id being referenced is the parent's id, because for COPPA requirements, I don't have it as a separate record) Thank you!

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.