简体   繁体   English

错误:useHref() 只能在 a 的上下文中使用<Router>零件。 在注册中,js

[英]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.

相关问题 错误:useHref() 只能在 a 的上下文中使用<router>测试时的组件</router> - Error: useHref() may be used only in the context of a <Router> component when testing 如何修复错误:useHref() 只能在 a 的上下文中使用<router>零件</router> - How to fix Error: useHref() may be used only in the context of a <Router> component 错误:useHref() 只能在 a 的上下文中使用<router>成分</router> - Error: useHref() may be used only in the context of a <Router> component 错误:useHref() 只能在 a 的上下文中使用<router>成分。 当我直接将 url 作为 localhost:3000/experiences 时,它会起作用</router> - Error: useHref() may be used only in the context of a <Router> component. It works when I directly put the url as localhost:3000/experiences 我如何解析“ useHref() 只能在上下文中使用<router>组件”当组件已经在里面时<router> ?</router></router> - How can I resolver " useHref() may be used only in the context of a <Router> component" when component is already inside <Router>? 未捕获的错误:useNavigate() 只能在<router>成分。 - 我做错了什么?</router> - Uncaught Error: useNavigate() may be used only in the context of a <Router> component. - What is it that I am doing wrong? React js:错误:useLocation()只能在a的上下文中使用<router>成分</router> - React js: Error: useLocation() may be used only in the context of a <Router> component 反应路由器错误“useRoutes()只能用于<router>成分。”</router> - React router error "useRoutes() may be used only in the contect of a <Router> component." 未捕获的错误:useLocation() 只能在 a 的上下文中使用<router>零件</router> - Uncaught Error: useLocation() may be used only in the context of a <Router> component 错误:useRoutes() 只能在<Router>零件 - Error: useRoutes() may be used only in the context of a <Router> component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM