0

I use React.js and I am getting this error while navigate in protected route from SocialLogin component:

 Warning: Cannot update a component (`BrowserRouter`) while rendering a different component (`SocialLogin`). To locate the bad setState() call inside `SocialLogin`.

I went to the locations mentioned in the stack trace and removed all setstates but it's still giving me same error. How can I remove this error?

Here is my react component code:

SocialLogin.js

const SocialLogin = () => {
const navigate = useNavigate();
let location = useLocation();

let from = location.state?.from?.pathname || "/";
const [signInWithGoogle, user] = useSignInWithGoogle(auth);
const handleSocialSignIn = () => {
    signInWithGoogle();
}
if (user) {
    navigate(from, { replace: true });
}
 return (
    <div onClick={handleSocialSignIn}>
        <img src={google} alt="" />
        <span className='signin-text'>Sign-in With Google</span>
    </div>
  );
};

export default SocialLogin;

1 Answer 1

2

Try this:

  useEffect(() => {
    if (user) {
      navigate(from, { replace: true });
    }
  }, [user]);
Sign up to request clarification or add additional context in comments.

2 Comments

This work properly and remove the error .But after Sign in it takes the user in home page . How can I take the user in protected route?
Update the navigate function to send the user to the route you want.

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.