use redirect path for signup but it not working. its redirect the home page and show a warning?

Viewed 37

see the code i navigate the path but not working its working for signup but its navigate the '/' router not navigate the pathname .

import { Link, useLocation, useNavigate } from "react-router-dom";
import useToken from "../../../hooks/useToken";

const [createUserWithEmailAndPassword, user, loading, error1] =
    useCreateUserWithEmailAndPassword(auth, { sendEmailVerification: true });
  let errorElement;
  const [token] = useToken(user);
  const navigate = useNavigate();
  const location = useLocation();

  const from = location.state?.from?.pathname || "/";
  if (token) {
    navigate(from, { replace: true });
  }```

Warning: Cannot update a component (`BrowserRouter`) while rendering a different component (`Signup`). To locate the bad setState() call inside `Signup`, follow the stack trace as described in.(react_devtools_backend.js:3973 )
show the warning for the navigation but i navigate the selected path not the home or '/'
1 Answers

The navigation action should be triggered from a useEffect hook as an intentional side-effect instead of in the component body as an unintentional effect.

Example:

useEffect(() => {
  if (token) {
    navigate(from, { replace: true });
  }
}, [navigate, token, from]);
Related