bradtraversy / mern-tutorial

Goalsetter application from Youtube series

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Infinite Loop

N3OGeorgy opened this issue · comments

dispatch(getGoals())
return () => {
dispatch(reset())
}

I am a newbie in react but there is a infinite loop issue if dispatch(getGoals()) gets rejected.
The new state will be state.isError = true from extraReducers getGoals.rejected.
Then dispatch(reset()) comes and sets the state state.isError = false, component renders again and the loop starts again.

My fix:
dispatch(getGoals()) if(!isError){ dispatch(reset()) }

If someone can come with a linked explanation with the exact reason why this is happening would be great :)

I am learning react too. I don't have your answer but how did your fix solve the problem?

I still haven't understand the ins and outs why the reset() causing the loop. But in my case, adding return after navigate to '/login' fix the problem.

It's a slight bug in the code. The reset function we import should be imported from authSlice instead of goalSlice, since we only want to reset stuff like isError, isSuccess, isLoading. Right now we are also resetting the goals array alongside which is resulting in an infinite loop.

Just fix the reset function import by changing goalSlice to authSlice and everything will work like a charm.

Solved !!!

if (user) {
      dispatch(getGoals());
    }

    return () => {
      dispatch(reset());
    };

Hi, I'm new to React, so consider this comment more of a personal understanding than the answer itself.
I got infinite loop when trying to logout user.
Testing with a version older than React 18 seems to work just fine without conditioning 'dispatch(getGoals())'.
I noticed that the actions in Redux DevTools are showing twice (which is not the case in Brads video) and then while I was trying to figure out why this is happening I came across this:
https://reactjs.org/docs/strict-mode.html#ensuring-reusable-state
Apparently, in strict mode and development environment, after mounting component "React will simulate immediately unmounting and remounting the component".
This may have caused a problem with the useEffect somehow getting in infinite loop in the Dashboard component with 'dispatch(getGoals())' exposed.
All of us newbies would appreciate if some more experienced developer could look into this and let us know if this makes any sense or am I on wrong trail.
infinite-loop
.

It's a slight bug in the code. The reset function we import should be imported from authSlice instead of goalSlice, since we only want to reset stuff like isError, isSuccess, isLoading. Right now we are also resetting the goals array alongside which is resulting in an infinite loop.

Just fix the reset function import by changing goalSlice to authSlice and everything will work like a charm.

So i was wondering if there is no user the navigate function will execute and then the dispatch? or it executes the return function?

instead of using a return which won't unmount I'll suggest this:

if (!user) {
    navigate('/login');
} else {
    dispatch(getGoals());
}

this way when the user isn't valid/logged in then we are not calling the dispatch(getGoals()) action which is what is causing the infinite loop