英文:
<Link> error on react-router-dom in React with Typescript
问题
I got a problem with the lib 'react-router-dom'. This is my first time using React with Typescript;
Everything is working fine until I put the from the 'react-router-dom' when all the page goes down;
Basically I want to put three items on the header (Home, About, and Login) and change the page when I click on them. The errors come when I put the tag on the component, if I just update the App.jsx and change the page writing on URL, it works fine.
App.jsx
import 'bootstrap/dist/css/bootstrap.min.css';
// --- component ---
import MainHeader from './components/MainHeader';
import MainFooter from './components/MainFooter';
// --- pages ---
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';
// --- style ---
import './App.css';
function App() {
return (
<div className="App">
<div className='sec-header'>
<MainHeader />
</div>
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</BrowserRouter>
<div className='sec-footer'>
<MainFooter />
</div>
</div>
);
}
export default App;
MainHeader
import './MainHeader.css';
import { Link } from 'react-router-dom';
function MainHeader() {
return (
<div className="p-3">
<nav className="navbar navbar-expand">
<ul className="rounded-4 d-flex m-auto p-0">
<li className="btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100">
<Link to="/">Home</Link>
</li>
<li className="btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100">
<Link to="/about">About</Link>
</li>
<li className="btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100">
<Link to="/login">Login</Link>
</li>
</ul>
</nav>
</div>
)
}
export default MainHeader
2 Errors on Console
-
Uncaught Error: useHref() may be used only in the context of a
component. -
react-dom.development.js:18687 The above error occurred in the component:
Do you know what it could be?
英文:
I got a problem with the lib 'react-router-dom'. This is my first time using React with Typescript;
Everything is working fine until I put the <Link> from the 'react-router-dom' when all the page goes down;
Basically I want to put three items on header (Home, About and login) and change the page when I click on them. The errors comes when I put the tag <Link> on the component, if I just update the App.jsx and change the page writing on URL, it works fine.
App.jsx
import 'bootstrap/dist/css/bootstrap.min.css';
// --- component ---
import MainHeader from './components/MainHeader';
import MainFooter from './components/MainFooter';
// --- pages ---
import { BrowserRouter, Routes, Route } from 'react-router-dom'
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';
// --- style ---
import './App.css';
function App() {
return (
<div className="App">
<div className='sec-header'>
<MainHeader />
</div>
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />}></Route>
<Route path="/about" element={<About />}></Route>
<Route path="/contact" element={<Contact />}></Route>
</Routes>
</BrowserRouter>
<div className='sec-footer'>
<MainFooter />
</div>
</div>
);
}
export default App;
MainHeader
import './MainHeader.css'
import { Link } from 'react-router-dom'
function MainHeader() {
return (
<div className="p-3">
<nav className="navbar navbar-expand">
<ul className="rounded-4 d-flex m-auto p-0">
<li className="btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100">
<Link to="/">Home</Link>
</li>
<li className="btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100">
<Link to="/about">About</Link>
</li>
<li className="btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100">
<Link to="/login">Login</Link>
</li>
</ul>
</nav>
</div>
)
}
export default MainHeader
2 Errors on Console
1. Uncaught Error: useHref() may be used only in the context of a <Router> component.
2. react-dom.development.js:18687 The above error occurred in the <Link> component:
Do you know what it coulkd be?
答案1
得分: 1
因为你的 MainHeader 组件在 app.jsx 文件中的 BrowserRouter 组件之外,所以可能出现这个问题。如果你想让页眉在所有页面上都可见,将它放在 BrowserRouter 组件内部,但是在 Routes 组件之外:
function App() {
return (
<div className="App">
<BrowserRouter>
<div className='sec-header'>
<MainHeader /> // 这一行在 BrowserRouter 内部
</div>
<Routes>
<Route path="/" element={<Home />}></Route>
<Route path="/about" element={<About />}></Route>
<Route path="/contact" element={<Contact />}></Route>
</Routes>
</BrowserRouter>
<div className='sec-footer'>
<MainFooter />
</div>
</div>
);
}
英文:
It's probably because your MainHeader component is outside of the BrowserRouter component in the app.jsx file. If you want your header to be seen on all your pages, put it inside your BrowserRouter component, but outside your Routes component:
function App() {
return (
<div className="App">
<BrowserRouter>
<div className='sec-header'>
<MainHeader /> //THIS LINE HERE INSIDE BROWSERROUTER
</div>
<Routes>
<Route path="/" element={<Home />}></Route>
<Route path="/about" element={<About />}></Route>
<Route path="/contact" element={<Contact />}></Route>
</Routes>
</BrowserRouter>
<div className='sec-footer'>
<MainFooter />
</div>
</div>
);
}
答案2
得分: 1
将MainHeader组件移至BrowserRouter内以解决此问题:
import 'bootstrap/dist/css/bootstrap.min.css';
// --- component ---
import MainFooter from './components/MainFooter';
// --- pages ---
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';
// --- style ---
import './App.css';
function App() {
return (
<div className="App">
<BrowserRouter>
<div className='sec-header'>
<MainHeader />
</div>
<Routes>
<Route path="/" element={<Home />}></Route>
<Route path="/about" element={<About />}></Route>
<Route path="/contact" element={<Contact />}></Route>
</Routes>
</BrowserRouter>
<div className='sec-footer'>
<MainFooter />
</div>
</div>
);
}
英文:
Move Mainheader component inside BrowserRouter as below to fix this issue
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
import 'bootstrap/dist/css/bootstrap.min.css';
// --- component ---
import MainHeader from './components/MainHeader';
import MainFooter from './components/MainFooter';
// --- pages ---
import { BrowserRouter, Routes, Route } from 'react-router-dom'
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';
// --- style ---
import './App.css';
function App() {
return (
<div className="App">
<BrowserRouter>
<div className='sec-header'>
<MainHeader />
</div>
<Routes>
<Route path="/" element={<Home />}></Route>
<Route path="/about" element={<About />}></Route>
<Route path="/contact" element={<Contact />}></Route>
</Routes>
</BrowserRouter>
<div className='sec-footer'>
<MainFooter />
</div>
</div>
);
}
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论