Throttling navigation to prevent the browser from hanging. See https://crbug.com/882238. Command line switch --disable-ipc-flooding-protection can be used to bypass the protection
这是它抛出的完整警告:
我的代码:
import { login, resetState } from "../../store/auth/authActions";
import { isAuthenticated } from "../../utils/auth";
const Login = ({ history }) => {
const dispatch = useDispatch();
//joi schema
....
//react hook form setup
const {
register,
handleSubmit,
setError,
clearErrors,
formState: { errors },
} = useForm({
resolver: joiResolver(schema),
});
useEffect(() => {
dispatch(resetState(clearErrors));
if (isAuthenticated) {
//AS SOON AS I UNCOMMENT THE NEXT LINE THROWS THE WARNING IN LOOP
// history.push("/dashboard");
}
}, [clearErrors, dispatch, history]);
const onSubmit = (data) => {
dispatch(resetState(clearErrors));
dispatch(login(data));
};
// global state
const serverSideErrors = useSelector((state) => state.auth.errors);
useEffect(() => {
Object.entries(serverSideErrors).forEach(([i, v]) => {
setError(i, {
message: v[0],
});
});
}, [serverSideErrors, setError]);
return (...)
export default Login;
在这里,您可以看到一行:history.push("/dashboard");
这似乎是导致问题的原因。如果我注释这一行。这一警告将会消失。
我还尝试过:
useEffect(() => {
dispatch(resetState(clearErrors));
if (isAuthenticated) {
history.push("/dashboard");
}
}, []);
但问题是一样的。我怎么能解决这个问题呢?我在这里错过了什么?
发布于 2021-06-20 12:43:47
我的猜测是dispatch(resetState(clearErrors));
改变了clearErrors
引用。由于clearErrors
是同一个钩子的依赖项,并且它在钩子中更改,因此它在循环中被调用,并重复运行history.push("/dashboard");
。
你应该有条件地执行dispatch(resetState(clearErrors));
,否则它总是会在循环中结束。
PS:如果你能发布一个这个问题的最低限度的回购,我也许能更好地帮助你。
https://stackoverflow.com/questions/68052206
复制