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;