[英]React Hook useEffect has a missing dependency: 'History'
I Have This Code:我有这个代码:
import {useContext, useEffect, useState} from 'react';
import {useHistory} from "react-router-dom";
import {MasterContext} from "../../Context/MasterProvider";
import LoginActions from "../../Context/Actions/LoginActions";
const useLoginForm = () => {
const History = useHistory();
const [login, setLogin] = useState({});
const {AuthState: {Authentication: {Loading, Data, Error}}, AuthDispatch}=useContext(MasterContext);
const FormData = (event) => {
const { target: { value, name } } = event;
setLogin({...login, [name]: value});
};
const FormValid =
!login.email?.length ||
!login.password?.length;
const FormSubmit = () => {
LoginActions(login)(AuthDispatch);
}
useEffect(() => {
if(Data) {
if(Data.user) {
History.push("/");
}
}
}, [Data])
return {login, FormData, FormValid, FormSubmit, Loading, Error, Data};
}
export default useLoginForm;
It's work fine but with warnings.它工作正常,但有警告。 "React Hook useEffect has a missing dependency: 'History'. Either include it or remove the dependency array react-hooks/exhaustive-deps"
“React Hook useEffect 缺少依赖项:'History'。要么包含它,要么删除依赖数组 react-hooks/exhaustive-deps”
You can add History as a dependency, History wont change unless route is changed.您可以将历史记录添加为依赖项,除非更改路线,否则历史记录不会改变。 So your useEffect hook wont run unless data or History is changed.
因此,除非更改数据或历史记录,否则您的 useEffect 挂钩将不会运行。
useEffect(() => {
if(Data && Data.user) {
History.push("/");
}
}, [Data, History])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.