gpt4 book ai didi

reactjs - 错误 : useHref() may be used only in the context of a component

转载 作者:行者123 更新时间:2023-12-05 01:27:16 25 4
gpt4 key购买 nike

**当我直接在我的路由器组件中编写我的导航栏组件内容时。它工作正常但是当我在 NavBar 组件中写入该内容时它会生成以下错误

错误:useHref() 只能在组件的上下文中使用。.**

我正在使用 "react-dom": "^17.0.2", "react-router-dom": "^6.0.0-beta.6",

以下是我的组件..

我的导航栏组件:-

import { Link } from "react-router-dom";
// import styles from './NavBar.module.css';
export const NavBar = () => {
return (
<nav>
<Link to="/"> Home </Link>
<Link to="/about"> About </Link>
<Link to="/product"> Product </Link>
</nav>
);
}

我的路线组件:-

import { BrowserRouter as Router, Switch, Route, Routes} from "react-router-dom";

// importing Component's
import { Home } from "./Components/Home/Home";
import { About } from "./Components/About/About";
import { Products } from "./Components/Products/Products";
import { ProductItems } from "./Components/ProductItems/ProductItems";
import {NavBar} from "./Components/NavBar/NavBar";
import { Fotter } from "./Components/Fotter/Fotter";

export const RouterConfig = () => {
return (
<Router>
<NavBar />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route exact path="/product" component={Products} />
<Route path="/product/:id" component={ProductItems} />
<Route path="*" component={() => <h2>404 Not Found </h2>} />
</Switch>
{/* <Fotter />' */}
</Router>
);
};

最佳答案

import {BrowserRouter as Router} from "react-router-dom";

在 index.js 中的 App.js 或以上组件中使用,并将您的组件包装在 Router 中。然后问题就会消失;-)在您的情况下,我看到两个选项:

  1. index.js

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { BrowserRouter as Router } from "react-router-dom";

ReactDOM.render(
<React.StrictMode>
<Router>
<App />
</Router>
</React.StrictMode>,
document.getElementById("root")
);

使用App组件代替RouterConfig组件。

  1. App.js

import { BrowserRouter as Router, Switch, Route, Routes} from "react-router-dom";

// importing Component's
import { Home } from "./Components/Home/Home";
import { About } from "./Components/About/About";
import { Products } from "./Components/Products/Products";
import { ProductItems } from "./Components/ProductItems/ProductItems";
import {NavBar} from "./Components/NavBar/NavBar";
import { Fotter } from "./Components/Fotter/Fotter";

export const App = () => {
return (
<Router>
<NavBar />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route exact path="/product" component={Products} />
<Route path="/product/:id" component={ProductItems} />
<Route path="*" component={() => <h2>404 Not Found </h2>} />
</Switch>
{/* <Fotter />' */}
</Router>
);
};

我尽力解释了它。希望这对你有帮助 ;-) 最好的问候和好运!如果您仍然感到困惑,请查看此处 => React Router V6 - Error: useRoutes() may be used only in the context of a <Router> component

关于reactjs - 错误 : useHref() may be used only in the context of a <Router> component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69508954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com