反应 history.push 到 localhost:3000 不重定向
React history.push to localhost:3000 not redirecting
我正在使用 React 创建登录页面。 history.push("/") 没有引导,控制台中出现此错误:未捕获类型错误:无法读取未定义的属性(读取 'push')。
从教程中做到这一点并完全按照代码进行操作,但几天仍然无法弄清楚。
import { useState } from "react";
import styles from "./register.css";
import Login from "../../containers/login/index";
function Register({ props, history }) {
const [name, setName] = useState();
const [email, setEmail] = useState();
const [password, setPassword] = useState();
function registerClicked() {
if (name !== " " && email !== " " && password !== " ") {
console.log(name, email, password);
alert("Thanks for registering");
history.push("/");
} else {
alert("Please register to continue");
}
}
return(
<div className="regContainer">
<p className="registerTitle">Register to sign in</p>
<input type="text"
placeholder="Create username"
onChange={(nameText) => setName(nameText.target.value)} />
<input type="text"
placeholder="Email address"
onChange={(emailText) => setEmail(emailText.target.value)} />
<input type="password"
placeholder="Password"
onChange={(passwordText) => setPassword(passwordText.target.value)} />
<button onClick={() => registerClicked()}>Register</button>
<p className="loginInstead">Already have an account? Sign in instead</p>
</div>
);
}
export default Register;
import { useNavigate } from "react-router-dom"
export default const Register = () => {
const navigate = useNavigate();
if(...){
navigate('/')
}
return <div></div>
}
我正在使用 React 创建登录页面。 history.push("/") 没有引导,控制台中出现此错误:未捕获类型错误:无法读取未定义的属性(读取 'push')。
从教程中做到这一点并完全按照代码进行操作,但几天仍然无法弄清楚。
import { useState } from "react";
import styles from "./register.css";
import Login from "../../containers/login/index";
function Register({ props, history }) {
const [name, setName] = useState();
const [email, setEmail] = useState();
const [password, setPassword] = useState();
function registerClicked() {
if (name !== " " && email !== " " && password !== " ") {
console.log(name, email, password);
alert("Thanks for registering");
history.push("/");
} else {
alert("Please register to continue");
}
}
return(
<div className="regContainer">
<p className="registerTitle">Register to sign in</p>
<input type="text"
placeholder="Create username"
onChange={(nameText) => setName(nameText.target.value)} />
<input type="text"
placeholder="Email address"
onChange={(emailText) => setEmail(emailText.target.value)} />
<input type="password"
placeholder="Password"
onChange={(passwordText) => setPassword(passwordText.target.value)} />
<button onClick={() => registerClicked()}>Register</button>
<p className="loginInstead">Already have an account? Sign in instead</p>
</div>
);
}
export default Register;
import { useNavigate } from "react-router-dom"
export default const Register = () => {
const navigate = useNavigate();
if(...){
navigate('/')
}
return <div></div>
}