【发布时间】:2020-08-26 10:00:27
【问题描述】:
我正在使用react-redux,目前我的全局状态中有一个布尔型isAuthenticated 属性,如果这是真的,它将重定向到/home 组件。
我实施的方法有更好的方法吗?如何解决输出错误?
import React, { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { login } from "./actions";
import { useHistory } from "react-router-dom"
const Login = () => {
const dispatch = useDispatch();
const history = useHistory();
const myState = useSelector(
state => state.authentication
);
const onSubmit = (values) => {
dispatch(login(values.email, values.password));
};
useEffect(() => {
if (myState.isAuthenticated) {
history.push("/home")
}
}, [isAuthenticated]);
}
警告:React Hook useEffect 缺少依赖项:'history'。要么包含它,要么移除依赖数组
[isAuthenticated, history]这可以接受吗?
【问题讨论】:
标签: javascript reactjs