简体   繁体   English

错误:不变失败:你不应该使用<Link>外面<Router>当我尝试使用 localhost 测试我的应用程序时

[英]Error: Invariant failed: You should not use <Link> outside a <Router> when I try to test my app using localhost

I'm getting an error when I try to test my app on localhost:3000 in the Router, how can I fix it?当我尝试在路由器的 localhost:3000 上测试我的应用程序时出现错误,我该如何解决? I'm newbie in React & React Router also, what kind of problems can cause this.我也是 React 和 React Router 的新手,什么样的问题会导致这种情况。

App.js:应用程序.js:

import { NavBar } from "./shared/NavBar"
import { Switch, Route } from "react-router-dom";
import { BooksList } from "./BooksList";
import { CreateBook } from "./CreateBook";
import { UpdateBook } from "./UpdateBook";

function App() {
    return (
        <>
            <NavBar />
            <Switch>
                <Route path="/create-book">
                    <CreateBook />
                </Route>
                <Route path="/update-book">
                    <UpdateBook />
                </Route>
                <Route path="/">
                    <BooksList />
                </Route>
            </Switch>
        </>
    );
}

export default App;

NavBar component:导航栏组件:

import { Flex, Image, Box, Link as StyledLink } from "rebass/styled-components";
import { Link } from "react-router-dom";

export const NavBar = () => {
    return (
        <Flex bg="black" color="white" justifyContent="center">
                <Flex px={2} width="100%" alignItems="center">
                    <Link component={StyledLink} variant="nav" to="/">
                        React CRUD App
                    </Link>
                    <Box mx="auto" />
                        <Link component={StyledLink} variant="nav" to="/create-book">
                            Add new item
                        </Link>
                </Flex>
        </Flex>
    )
}

Wrap your routing to BrowserRouter将您的路由包装到BrowserRouter

import进口

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

and wrap routes并包装路线

function App() {
    return (
        <Router>
           <div>
            <NavBar />
            <Switch>
                <Route path="/create-book">
                    <CreateBook />
                </Route>
                <Route path="/update-book">
                    <UpdateBook />
                </Route>
                <Route path="/">
                    <BooksList />
                </Route>
            </Switch>
           </div>
        </Router>
    );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React - 错误:不变量失败:你不应该使用<link>外面<router></router> - React - Error: Invariant failed: You should not use <Link> outside a <Router> 错误:不变量失败:您不应该使用<link>外面<router>任何人?</router> - Error: Invariant failed: You should not use <Link> outside a <Router> Anyone? 错误:不变量失败:您不应该使用<link>外面<router></router> - Error: Invariant failed: You should not use <Link> outside a <Router> 错误:不变量失败:您不应该使用<link>外面<router>在反应</router> - Error: Invariant failed: You should not use <Link> outside a <Router> in react 错误:不变量失败:您不应该使用<withrouter(app) />外面<router></router> - Error: Invariant failed: You should not use <withRouter(App) /> outside a <Router> 在 Mapbox GL JS 弹出窗口中使用链接会引发错误:不变式失败:您不应该使用<link>外面<router></router> - Using Link in Mapbox GL JS popup throws Error: Invariant failed: You should not use <Link> outside a <Router> 我的代码中出现此错误-&gt;未捕获错误:不变失败:您不应该使用<Link>外面<Router> ,有人可以看到问题吗? - i'm getting this error in my code --> Uncaught Error: Invariant failed: You should not use <Link> outside a <Router>, can someone see the problem? “链接”的 React Router 问题,错误:不变失败:您不应该使用<link>外面<router></router> - React Router problem with "Link", Error: Invariant failed: You should not use <Link> outside a <Router> 是否强制使用<route>仅在 App.js 内部? 抛出错误,如“错误:不变失败:您不应该使用<link>外面<router> "</router></route> - Is it compulsory to use <Route> only inside of App.js? throwing error like "Error: Invariant failed: You should not use <Link> outside a <Router>" 不变量失败:您不应该使用<link>外面<router></router> - Invariant failed: You should not use <Link> outside a <Router>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM