简体   繁体   English

React App 抛出 React 路由器 `useLocation()` 错误

[英]React App throwing a react router `useLocation()` error

I've Created a component which works in Storybook, it's been published to a private repo.我已经创建了一个在 Storybook 中工作的组件,它已经发布到一个私人仓库。 When I consume this navigation component which uses <navLink> I get a Uncaught Error: useLocation() may be used only in the context of a <Router> component .当我使用这个使用<navLink>的导航组件时,我得到一个Uncaught Error: useLocation() may be used only in the context of a <Router> component I've searched for this and keep getting the same answer.我搜索过这个并不断得到相同的答案。 Which is to warp the app in a <BrowserRouter> tag.这是在<BrowserRouter>标签中扭曲应用程序。 However my app IS wrapped in a <BrowserRouter> tag and I still get this error.但是我的应用程序包含在<BrowserRouter>标签中,我仍然收到此错误。 Any help would be welcome as I'm banging my head off the screen here.任何帮助都将受到欢迎,因为我正在将我的头从屏幕上移开。

Index file索引文件

import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import ReactDOM from 'react-dom/client';
import App from './components/Container/App';
import store from './app/store';
import { Provider } from 'react-redux';

const root = ReactDOM.createRoot(document.getElementById('root') || new DocumentFragment());
root.render(
  <React.StrictMode>
    <Provider store={store}>
      <BrowserRouter>
        <App />
      </BrowserRouter>
    </Provider>
  </React.StrictMode>
);

App file应用文件

import React from 'react';
import { Routes, Route } from 'react-router-dom';
import PageLayout from '../UI/PageLayout/PageLayout';
import Home from '../Home/Home';
import Portal from '../Portal/Portal';
import ProductSearch from '../ProductSearch/ProductSearch';
import { InternalRoutes } from '../../common/enums/internal-routes';
import Inbox from '../Inbox/Inbox';

const App = () => {
  return (
    <PageLayout >
      <Routes>
        <Route path={InternalRoutes.Default} element={<Home />} />
        <Route path={InternalRoutes.Home} element={<Home />} />
        <Route path={InternalRoutes.Inbox} element={<Inbox />} />
        <Route path={InternalRoutes.Portal} element={<Portal />} />
        <Route path={InternalRoutes.ProductSearch} element={<ProductSearch />} />
      </Routes>             
    </PageLayout>
  );
};

export default App;

PageLayout file页面布局文件

import React, { FC, ReactNode } from 'react';
import { Logo, PrimaryNavigation } from 'my-component-lib';
import { InternalRoutes as Routes } from '../../../common/enums/internal-routes';

interface I_PageLayoutProps {
  children: ReactNode;
}

const PageLayout:FC<I_PageLayoutProps> = ({children}) => {
  return (
    <div className="body_grid">
      <header>
        <Logo />
        <PrimaryNavigation navigationLinks={Object.values(Routes) as unknown as Array<keyof typeof Routes>} />
      </header>
      <div className={'main_Grid'} >{children}</div>
    </div>
  );
};

export default PageLayout;

You can solve this problem by removing <BrowserRouter> from Index file and wrapping <Routes> tag in App file您可以通过从Index文件中删除<BrowserRouter>并将<Routes>标记包装App文件中来解决此问题

New Index新指数

   import React from 'react';
    import ReactDOM from 'react-dom/client';
    import App from './components/Container/App';
    import store from './app/store';
    import { Provider } from 'react-redux';

    const root = ReactDOM.createRoot(document.getElementById('root') || new DocumentFragment());
    root.render(
        <React.StrictMode>
            <Provider store={store}>
                    <App />
            </Provider>
        </React.StrictMode>
    );

New App file新建应用文件

import React from 'react';
import { Routes, Route,BrowserRouter  } from 'react-router-dom';
import PageLayout from '../UI/PageLayout/PageLayout';
import Home from '../Home/Home';
import Portal from '../Portal/Portal';
import ProductSearch from '../ProductSearch/ProductSearch';
import { InternalRoutes } from '../../common/enums/internal-routes';
import Inbox from '../Inbox/Inbox';

const App = () => {

    return (
        <PageLayout >
          <BrowserRouter>
            <Routes>
                <Route path={InternalRoutes.Default} element={<Home />} />
                <Route path={InternalRoutes.Home} element={<Home />} />
                <Route path={InternalRoutes.Inbox} element={<Inbox />} />
                <Route path={InternalRoutes.Portal} element={<Portal />} />
                <Route path={InternalRoutes.ProductSearch} element={<ProductSearch />} />
            </Routes> 
           </BrowserRouter>              
        </PageLayout>
    );
};

export default App;

I hope this will solve the problem我希望这能解决问题

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM