[英]Error: useHref() may be used only in the context of a <Router> component. in register,js
Good afternoon, I am creating an app in reactjs and all the routes work fine, only the register user gives the error Error: useHref() may be used only in the context of a component.I am using firebase for this, the login with google and user and password works 100%, thanks下午好,我正在 reactjs 中创建一个应用程序,所有路由都工作正常,只有注册用户给出错误错误:useHref() 只能在组件的上下文中使用。我为此使用 firebase,登录谷歌和用户和密码工作 100%,谢谢
register.js code register.js 代码
import { push } from "firebase/database";
import React, { useEffect, useState } from "react";
import { useAuthState } from "react-firebase-hooks/auth";
import { Link, Route, useHistory } from "react-router-dom";
import {
auth,
registerWithEmailAndPassword,
signInWithGoogle,
} from "../config/firebase";
function Register() {
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
const [name, setName] = useState("");
const [user, loading, error] = useAuthState(auth);
const register = () => {
if (!name) alert("Please enter name");
registerWithEmailAndPassword(name, email, password);
};
useEffect(() => {
if (loading) return;
if (user) Route.push("/dashboard");
}, [user, loading]);
return (
<div className="register">
<div className="register__container">
<input
type="text"
className="register__textBox"
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Full Name"
/>
<input
type="text"
className="register__textBox"
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="E-mail Address"
/>
<input
type="password"
className="register__textBox"
value={password}
onChange={(e) => setPassword(e.target.value)}
placeholder="Password"
/>
<button className="register__btn" onClick={register}>
Register
</button>
<button
className="register__btn register__google"
onClick={signInWithGoogle}
>
Register with Google
</button>
<div>
Already have an account? <Link to="/">Login</Link> now.
</div>
</div>
</div>
);
}
export default Register;
You forgot to wrap your React App
within BrowserRouter
or some router.您忘记将React App
包装在BrowserRouter
或某些路由器中。 Go to index.js
in src folder.转到 src 文件夹中的index.js
。 Wrap it like this.像这样包起来。
<BrowserRouter>
<App />
</BrowserRouter>
Then, add some Route
.然后,添加一些Route
。 For example, like this.例如,像这样。
<BrowserRouter>
<Routes>
<Route element={<Home/>} path={"/"} />
<Route element={<Another />} path={"/another}/>
</Routes>
</BrowserRouter>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.