[英]getting invalid hook call in react native useEffect()
I want to post the data from axios but I am getting invalid hook call error.我想从 axios 发布数据,但我收到无效的挂钩调用错误。 What I am doing wrong here.
我在这里做错了什么。 Here's my code
这是我的代码
const Login = (props) => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const onLoginSubmit = (event) => {
event.preventDefault();
const url = baseUrl + '/';
const loginData = {
email: email,
password: password,
};
setIsLoading(true);
useEffect(async () => {
await axios
.post(`${url}users/login`, loginData)
.then((res) => {
console.log(res.data);
AsyncStorage.setItem('token', JSON.stringify(res.data.token));
props.navigation.navigate('MainHome');
})
.catch((error) => {
setError(error.res.data);
setLoading(false);
setIsLoading(false);
});
});
};
You must call hooks on top-level, see Rules Of Hooks .您必须在顶层调用钩子,请参阅钩子规则。
Don't call Hooks inside loops, conditions, or nested functions .
不要在循环、条件或嵌套函数中调用 Hooks。
const Login = (props) => {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [error, setError] = useState("");
const [loading, setLoading] = useState(false);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
const fetch = async () => {
const url = baseUrl + "/";
const loginData = {
email: email,
password: password,
};
await axios
.post(`${url}users/login`, loginData)
.then((res) => {
console.log(res.data);
AsyncStorage.setItem("token", JSON.stringify(res.data.token));
props.navigation.navigate("MainHome");
})
.catch((error) => {
setError(error.res.data);
setLoading(false);
setIsLoading(false);
});
};
if (loading) {
fetch();
}
}, [loading, email, password]);
const onLoginSubmit = (event) => {
event.preventDefault();
setIsLoading(true);
};
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.