Skip to content
Advertisement

react router dom pushing user before finishing action

I’m building a Reddit clone with MERN and am using redux and react in frontend

So the problem is when a user tries to register it dosen’t push the user to “/login” route. This is not an issue with redux (i think) because after pressing the “register” button again with a different email it works

still here is the redux code just in case

state/actions/auth.js

JavaScript

state/reducers/authReducer.js

JavaScript

state/reducers/errorReducer.js

JavaScript

state/reducers/index.js

JavaScript

/pages/Register.js

JavaScript

/backend/authentication/auth.js

JavaScript

Advertisement

Answer

The issue is that handleOnClick doesnt wait till the execution of redux store update. Therefore, the history.push() doesnt run in the first case.

When you click submit again, there is already previous state in store. Therefore the if condition gets executed.

To resolve it you have to add a useEffect() hook which would redirect user after they register.

Example

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement