<Link> error on react-router-dom in React with Typescript

huangapple go评论55阅读模式
英文:

<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

  1. Uncaught Error: useHref() may be used only in the context of a component.

  2. 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 &#39;bootstrap/dist/css/bootstrap.min.css&#39;;

// --- component ---
import MainHeader from &#39;./components/MainHeader&#39;;
import MainFooter from &#39;./components/MainFooter&#39;;

// --- pages ---
import { BrowserRouter, Routes, Route } from &#39;react-router-dom&#39;
import Home from &#39;./pages/Home&#39;;
import About from &#39;./pages/About&#39;;
import Contact from &#39;./pages/Contact&#39;;

// --- style ---
import &#39;./App.css&#39;;

function App() {
    return (
        &lt;div className=&quot;App&quot;&gt;
            &lt;div className=&#39;sec-header&#39;&gt;
                &lt;MainHeader /&gt;
            &lt;/div&gt;
            &lt;BrowserRouter&gt;
                &lt;Routes&gt;
                    &lt;Route path=&quot;/&quot; element={&lt;Home /&gt;}&gt;&lt;/Route&gt;
                    &lt;Route path=&quot;/about&quot; element={&lt;About /&gt;}&gt;&lt;/Route&gt; 
                    &lt;Route path=&quot;/contact&quot; element={&lt;Contact /&gt;}&gt;&lt;/Route&gt; 
                &lt;/Routes&gt;
            &lt;/BrowserRouter&gt;
            &lt;div className=&#39;sec-footer&#39;&gt;
                &lt;MainFooter /&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    );
}

export default App;

MainHeader

import &#39;./MainHeader.css&#39;
import { Link } from &#39;react-router-dom&#39;

function MainHeader() {

    return (
        &lt;div className=&quot;p-3&quot;&gt;
            &lt;nav className=&quot;navbar navbar-expand&quot;&gt;
                &lt;ul className=&quot;rounded-4 d-flex m-auto p-0&quot;&gt;
                    &lt;li className=&quot;btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100&quot;&gt;
                        &lt;Link to=&quot;/&quot;&gt;Home&lt;/Link&gt;
                    &lt;/li&gt;
                    &lt;li className=&quot;btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100&quot;&gt;
                        &lt;Link to=&quot;/about&quot;&gt;About&lt;/Link&gt;
                    &lt;/li&gt;
                    &lt;li className=&quot;btn btn-header fs-5 p-0 py-2 px-4 mx-3 border-bottom rounded-0 w-100&quot;&gt;
                        &lt;Link to=&quot;/login&quot;&gt;Login&lt;/Link&gt;
                    &lt;/li&gt;
                &lt;/ul&gt;
            &lt;/nav&gt;
        &lt;/div&gt;
    )
}

export default MainHeader

2 Errors on Console

1. Uncaught Error: useHref() may be used only in the context of a &lt;Router&gt; component.

2. react-dom.development.js:18687 The above error occurred in the &lt;Link&gt; 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 (
    &lt;div className=&quot;App&quot;&gt;
        &lt;BrowserRouter&gt;
            &lt;div className=&#39;sec-header&#39;&gt;
                &lt;MainHeader /&gt; //THIS LINE HERE INSIDE BROWSERROUTER
            &lt;/div&gt;
            &lt;Routes&gt;
                &lt;Route path=&quot;/&quot; element={&lt;Home /&gt;}&gt;&lt;/Route&gt;
                &lt;Route path=&quot;/about&quot; element={&lt;About /&gt;}&gt;&lt;/Route&gt; 
                &lt;Route path=&quot;/contact&quot; element={&lt;Contact /&gt;}&gt;&lt;/Route&gt; 
            &lt;/Routes&gt;
        &lt;/BrowserRouter&gt;
        &lt;div className=&#39;sec-footer&#39;&gt;
            &lt;MainFooter /&gt;
        &lt;/div&gt;
    &lt;/div&gt;
);

}

答案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 &#39;bootstrap/dist/css/bootstrap.min.css&#39;;

// --- component ---
import MainHeader from &#39;./components/MainHeader&#39;;
import MainFooter from &#39;./components/MainFooter&#39;;

// --- pages ---
import { BrowserRouter, Routes, Route } from &#39;react-router-dom&#39;
import Home from &#39;./pages/Home&#39;;
import About from &#39;./pages/About&#39;;
import Contact from &#39;./pages/Contact&#39;;

// --- style ---
import &#39;./App.css&#39;;

function App() {
    return (
        &lt;div className=&quot;App&quot;&gt;
           &lt;BrowserRouter&gt;
            &lt;div className=&#39;sec-header&#39;&gt;
                &lt;MainHeader /&gt;
            &lt;/div&gt;

                &lt;Routes&gt;
                    &lt;Route path=&quot;/&quot; element={&lt;Home /&gt;}&gt;&lt;/Route&gt;
                    &lt;Route path=&quot;/about&quot; element={&lt;About /&gt;}&gt;&lt;/Route&gt; 
                    &lt;Route path=&quot;/contact&quot; element={&lt;Contact /&gt;}&gt;&lt;/Route&gt; 
                &lt;/Routes&gt;
            &lt;/BrowserRouter&gt;
            &lt;div className=&#39;sec-footer&#39;&gt;
                &lt;MainFooter /&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    );
}

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月14日 06:58:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441970.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定