Reactjs 如何将其转换为与我';你准备好了吗?
我已经成功地将我的大部分代码转换为React hook with Node,用于服务器对mysql数据库进行写/读操作。但是,我需要能够以某种方式使用此代码来验证用户名和密码:Reactjs 如何将其转换为与我';你准备好了吗?,reactjs,Reactjs,我已经成功地将我的大部分代码转换为React hook with Node,用于服务器对mysql数据库进行写/读操作。但是,我需要能够以某种方式使用此代码来验证用户名和密码: if (this.state.username === "Tim" && this.state.password === "1234") { AuthenticationService.registerSuccessfulLogin(
if (this.state.username === "Tim" && this.state.password === "1234") {
AuthenticationService.registerSuccessfulLogin(
this.state.username,
this.state.password
);
this.props.history.push(`/welcome/${this.state.username}`);
} else {
this.setState({ showSuccessMessage: false });
this.setState({ hasLoginFailed: true });
}
}
以下是我需要使用的代码:
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [loginStatus, setLoginStatus] = useState("");
const login = () => {
Axios.post("http://localhost:3000/login", {
username: username,
password: password,
}).then((response) => {
if (response.data.message) {
setLoginStatus(response.data.message)
} else {
setLoginStatus(response.data[0].username)
}
});
};
这是我尝试过的,但不起作用:
useEffect(() => {
const navigate = async () => {
const authorized = await
AuthenticationService.registerSuccessfulLogin;
if (username === "Tim" && password === "1234" && authorized) {
history.push(`/welcome/${username}`);
} else if (!authorized) {
history.push("/login");
}
};
navigate();
},[username,password,history])
这就是成功的原因:
const history = useHistory();
const login = () => {
Axios.post("http://localhost:3000/login", {
username: username,
password: password,
}).then((response) => {
if (response.data.message) {
setLoginStatus(response.data.message)
} else {
setLoginStatus(response.data[0].username)
AuthenticationService.registerSuccessfulLogin(
username,
password
);
history.push(`/welcome/${username}`);
}
});
}) 似乎您正在忙于调用
registerSuccessfullLogin
函数