看代码,我导航路径,但没有工作,它为注册工作,但它导航'/'路由器,而不是导航路径名。
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 '/'
导航动作应该由useEffect
钩子作为故意触发的副作用,而不是在组件体中作为无意的影响。
的例子:
useEffect(() => {
if (token) {
navigate(from, { replace: true });
}
}, [navigate, token, from]);