gpt4 book ai didi

reactjs - react - 错误 : Invariant failed: You should not use outside a

转载 作者:行者123 更新时间:2023-12-04 12:16:36 31 4
gpt4 key购买 nike

Stack Overflow 上有很多此错误的实例,但遗憾的是没有任何帮助调试问题。希望有人可以帮助我理解为什么,请参阅下面的代码。

Navbar.js:

import React from 'react';
import { Link } from 'react-router-dom'
const Navbar = ()=>{
return(
<nav className="nav-wrapper">
<div className="container">
<Link to="/" className="brand-logo">Shopping</Link>

<ul className="right">
<li><Link to="/">Shop</Link></li>
<li><Link to="/cart">My cart</Link></li>
<li><Link to="/cart"><i className="material-icons">shopping_cart</i></Link></li>
</ul>
</div>
</nav>
)
}

export default Navbar;

App.js:
import React from 'react';
import logo from './assets/logo.png';
import './styles/App.css';
import CartHeader from './components/CartHeader.js';
import Navbar from './components/Navbar.js';

function App() {
return (
<div className="App">
<Navbar />
<header className="App-header">
<div className="App-header-left">
<img src={logo} className="App-logo" alt="logo" />
</div>
<div className="App-header-right">
<CartHeader />
</div>
</header>

<body className="App-body">
<div className="App-body-image-container">
<h1 className="Green-header">Greenleaf</h1><h2 className="White-header">The apple of your eye.</h2>
</div>
</body>

</div>
);
}

export default App;

package.json:
在许多其他事情中, "react-router-dom": "^5.2.0" , 列在依赖项下。

应用程序本身编译没有错误,但错误:不变失败:您不应在 <路由器> 之外使用 <链接> 错误显示在浏览器中。有任何想法吗?

最佳答案

所以我可以看到你正在使用 <Link>并从正确的位置导入它,但是,您并没有包装您的 <App><Router> .

查看 react-router-dom额外支持的文档。

这是一个示例,说明如何 react-router-dom作品:

export default function App() {
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/users">Users</Link>
</li>
</ul>
</nav>

{/* A <Switch> looks through its children <Route>s and
renders the first one that matches the current URL. */}
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/users">
<Users />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</Router>
);
}

如果您注意到,链接位于路由器内部。

所以在你的情况下,你可能想要这样做:
import React from 'react';
import logo from './assets/logo.png';
import './styles/App.css';
import CartHeader from './components/CartHeader.js';
import Navbar from './components/Navbar.js';
import {
BrowserRouter as Router,
Switch,
Route,
Link
} from 'react-router-dom';
function App() {
return (
<Router>
<div className="App">
<Navbar />
<header className="App-header">
<div className="App-header-left">
<img src={logo} className="App-logo" alt="logo" />
</div>
<div className="App-header-right">
<CartHeader />
</div>
</header>

<body className="App-body">
<div className="App-body-image-container">
<h1 className="Green-header">Greenleaf</h1><h2 className="White-header">The apple of your eye.</h2>
</div>
</body>

</div>
<Router>
);
}

关于reactjs - react - 错误 : Invariant failed: You should not use <Link> outside a <Router>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61960971/

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