简体   繁体   English

React Router V6 - useNavigate() 只能在一个上下文中使用<router>零件</router>

[英]React Router V6 - useNavigate() may be used only in the context of a <Router> component

I have installed react-router-domV6.我已经安装了 react-router-domV6。 I am having the above error when I run my very basic test using Jest-Enzyme:当我使用 Jest-Enzyme 运行我的非常基本的测试时,我遇到了上述错误:

expect(shallow(<CustomerListTable customers={mockCustomers} />).length).toEqual(1);

I already came across this similar issue mentioned here , but I am already doing the provided answers.我已经遇到过这里提到的类似问题,但我已经在做提供的答案。 Even tried doing the import { BrowserRouter as Router } from "react-router-dom";甚至尝试import { BrowserRouter as Router } from "react-router-dom"; . . What am I missing here?我在这里想念什么?

index.js index.js

import React, { Suspense } from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { StrictMode } from "react";
import { BrowserRouter } from "react-router-dom";
...

ReactDOM.render(
  <StrictMode>
          ...
            <BrowserRouter>
              <AuthProvider>
                <App />
              </AuthProvider>
            </BrowserRouter>
          ...
  </StrictMode>,
  document.getElementById("root")
);

App.js应用程序.js

import React from "react";
import { useRoutes } from "react-router-dom";
import Routing from "./routes";
import useAuth from "./hooks/useAuth";
import { CreateCustomTheme } from "./theme";
import { ThemeProvider } from "@mui/material/styles";
import CssBaseline from "@mui/material/CssBaseline";
import useSettings from "./hooks/useSettings";
import useScrollReset from "./hooks/useScrollReset";
import SplashScreen from "./components/splashScreen/SplashScreen";

const App = () => {
  const content = useRoutes(Routing());
  const { settings } = useSettings();
  const auth = useAuth();

  useScrollReset();

  const theme = CreateCustomTheme({
    direction: settings.direction,
    responsiveFontSizes: settings.responsiveFontSizes,
    roundedCorners: settings.roundedCorners,
    theme: settings.theme,
  });
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      {auth.isInitialized ? content : <SplashScreen />}
    </ThemeProvider>
  );
};

export default App;

It seems the CustomerListTable component you are testing in isolation doesn't have access to a routing context it's expecting as when it's rendered in the app normally.您正在隔离测试的CustomerListTable组件似乎无法访问它所期望的路由上下文,就像它在应用程序中正常呈现时一样。 It's completely normal to need to wrap components when testing them with providers (redux, themes/styling, localization, routing , etc...) that are providing specific React Context values the component is accessing, typically via React hooks.使用提供组件正在访问的特定 React Context 值的提供程序(redux、主题/样式、本地化、路由等)测试组件时,需要包装组件是完全正常的,通常通过 React 挂钩。

For the unit test you should wrap the component with the providers it's expecting to have.对于单元测试,您应该将组件与它期望的提供者一起包装。

expect(shallow(
  <BrowserRouter>
    <CustomerListTable customers={mockCustomers} />
  </BrowserRouter>
).length).toEqual(1);

It's been a long time since I've used Enzyme, so I'm not certain the shallow render will still work, but if it doesn't then swap to the full mount test renderer.自从我使用 Enzyme 已经很长时间了,所以我不确定shallow渲染是否仍然有效,但如果它不起作用,则切换到全mount测试渲染器。

To fix this issue, you can direct mock your useNavigate() like this before all the imports for the file in which you are writing the test case:-要解决此问题,您可以在编写测试用例的文件的所有导入之前直接模拟您的 useNavigate() :-

const mockedUsedNavigate = jest.fn();
jest.mock('react-router-dom', () => ({
   ...jest.requireActual('react-router-dom'),
  useNavigate: () => mockedUsedNavigate,
}));

暂无
暂无

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

相关问题 (反应路由器 v6)“错误:useNavigate() 只能在<router>特定组件中的组件”错误</router> - (react router v6) "Error: useNavigate() may be used only in the context of a <Router> component" error in hoc component useNavigate() 只能在<router>零件</router> - useNavigate() may be used only in the context of a <Router> component React Router V6 - 错误:useRoutes() 只能在 a 的上下文中使用<router>成分</router> - React Router V6 - Error: useRoutes() may be used only in the context of a <Router> component 未捕获的错误:useNavigate() 只能在 a 的上下文中使用<router>零件</router> - Uncaught Error: useNavigate() may be used only in the context of a <Router> component useNavigate 外部功能组件 - react-router v6 - useNavigate outside functional component - react-router v6 React Router v6 useNavigate 与 MUI Drawer - React Router v6 useNavigate with MUI Drawer 未捕获的错误:useNavigate() 只能用于<Router> cypress 单元测试用例中的组件 - Uncaught Error: useNavigate() may be used only in the context of a <Router> component in cypress unit testcases React js:错误:useLocation()只能在a的上下文中使用<router>成分</router> - React js: Error: useLocation() may be used only in the context of a <Router> component React - react-router v6 在类组件中使用 Navigate 并设置状态 - React - react-router v6 useNavigate in class component and set state 与 TS 反应 - useNavigate() 与 react-router-dom v6 - react with TS - useNavigate() with react-router-dom v6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM