useHref() may be used only in the context of a <Router> component

在App.js路由最外层包一个BrowserRouter

import './App.css';

import React from "react";
import {BrowserRouter,Routes,Route,Link} from "react-router-dom";

import View1 from "./component/view1";
import View2 from "./component/view2";

function App() {
  return (
      <BrowserRouter>
          <div className='App'>
              <Link to='/view1'>view1</Link>
              <br/>
              <Link to='/view2'>view2</Link>
              <Routes>
                  <Route path='/view1' component={View1}/>
                  <Route path='/view2' component={View2}/>
              </Routes>
          </div>
      </BrowserRouter>
  );
}

export default App;

或者在index.js中App外包一个BrowserRouter

import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import {BrowserRouter} from "react-router-dom";

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
    <BrowserRouter>
        <App />
    </BrowserRouter>
  </React.StrictMode>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
reportWebVitals();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值