简体   繁体   English

如何修复错误:useHref() 只能在 a 的上下文中使用<router>零件</router>

[英]How to fix Error: useHref() may be used only in the context of a <Router> component

How do I resolve this problem.我该如何解决这个问题。 I am just trying to create a test the ensures that that component renders, but for some reason keep getting this problem even though the component is already inside <Router> .我只是想创建一个测试以确保该组件呈现,但由于某种原因,即使该组件已经在<Router>内部,也会出现此问题。

I have read other similar questions on here, and the answers all say to put the component inside the <Router/> , But that doesn't seem to be the issue for me.我在这里阅读了其他类似的问题,答案都说将组件放在<Router/>中,但这对我来说似乎不是问题。 Please tell me what it is I'm missing?请告诉我我错过了什么?

** My app.tsx** ** 我的 app.tsx **

import './App.scss';
import { AuthContexProvider } from './Utils/Contexs/AuthContext';
import { Routes, Route } from 'react-router-dom';
import { LogoLarge } from './Components/Logo/Logo';
import { SignInView } from './Views/SignInView';
import { ErrorPage } from './Views/ErrorView';
import { SignUpView } from './Views/SignUpView';
import { SwipeView } from './Views/SwipeView';
import { ResetPasswordView } from './Views/ResetPasswordView';
import { CreateProfileView } from './Views/CreateProfileView';
import { PrivateRoute } from './Utils/PrivateRoute';
import { ProfileView } from './Views/ProfileView';
import { SwipeContexProvider } from './Utils/Contexs/SwipeContex';
import { MatchesView } from './Views/MatchesView';
import { MessageView } from './Views/MessageView';

function App() {
  return (
    <div className='app'>
      <AuthContexProvider>
        <SwipeContexProvider>
          <header className='appWrapper'>
            <Routes>
              <Route path='/' element={<PrivateRoute component={SwipeView} />} />
              <Route
                path='/signin'
                element={
                  <div className='signInViewWrapper'>
                    <nav>
                      <LogoLarge />
                    </nav>
                    <SignInView />
                  </div>
                }
              />
              <Route
                path='/signup'
                element={
                  <div className='signUpViewWrapper'>
                    <nav>
                      <LogoLarge />
                    </nav>
                    <SignUpView />
                  </div>
                }
              />
              <Route
                path='/resetpassword'
                element={
                  <div className='resetPasswordViewWrapper'>
                    <nav>
                      <LogoLarge />
                    </nav>
                    <ResetPasswordView />
                  </div>
                }
              />
              <Route path='/createprofile' element={<PrivateRoute component={CreateProfileView} />} />
              <Route path='/profile' element={<PrivateRoute component={ProfileView} />} />
              <Route path='/matches' element={<PrivateRoute component={MatchesView} />} />
              <Route path='/matches/:id' element={<PrivateRoute component={MessageView} />} />
              <Route
                path='*'
                element={
                  <div className='appContent'>
                    <ErrorPage />
                  </div>
                }
              />
            </Routes>
          </header>
        </SwipeContexProvider>
      </AuthContexProvider>
    </div>
  );
}

export default App;

SignUpView注册查看

import { Typography } from '@mui/material';
import { Link } from 'react-router-dom';
import { SignUpForm } from '../Components/Forms/SignUpForm';

//SASS
// import '../Styles/Scss/SignUpView.scss';
import { StyledFormsWrapper } from '../Styles/StyledComponents/StyledFormsWrapper';

export const SignUpView = () => {
  return (
    <StyledFormsWrapper data-testid='todo-1' className='signUpWrapper'>
      <Typography className='actionTitle' variant='h5' gutterBottom component='div' sx={{ fontWeight: 600 }}>
        Create account
      </Typography>
      <Typography variant='subtitle1' gutterBottom component='div'>
        Already have an account? <Link to={'/signin'}>Sign in</Link>
      </Typography>
      <SignUpForm />
    </StyledFormsWrapper>
  );
};

SignUpView.test.js SignUpView.test.js

import React from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import { SignUpView } from '../../Views/SignUpView';

describe('my function or component', () => {
  test('Should render sigUp view component', () => {
    render(<SignUpView />);
  });
});

The SignUpView is missing a routing context in your test. SignUpView在您的测试中缺少路由上下文。 Import a memory router and wrap the component under test so it has a provided routing context.导入 memory 路由器并包装被测组件,使其具有提供的路由上下文。

import React from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import { MemoryRouter as Router } from 'react-router-dom';
import { SignUpView } from '../../Views/SignUpView';

describe('my function or component', () => {
  test('Should render sigUp view component', () => {
    render(
      <Router>
        <SignUpView />
      </Router>
    );
  });
});

for tests you can provide a container wrapper对于测试,您可以提供容器包装器

import { MemoryRouter } from 'react-router-dom';

test('Should render sigUp view component', () => {
  render(<SignUpView />, {wrapper: MemoryRouter});
});

this link has some useful examples 这个链接有一些有用的例子

暂无
暂无

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

相关问题 错误:useHref() 只能在 a 的上下文中使用<router>测试时的组件</router> - Error: useHref() may be used only in the context of a <Router> component when testing 我如何解析“ useHref() 只能在上下文中使用<router>组件”当组件已经在里面时<router> ?</router></router> - How can I resolver " useHref() may be used only in the context of a <Router> component" when component is already inside <Router>? 错误:useHref() 只能在 a 的上下文中使用<Router>零件。 在注册中,js - Error: useHref() may be used only in the context of a <Router> component. in register,js 错误:useHref() 只能在 a 的上下文中使用<router>成分。 当我直接将 url 作为 localhost:3000/experiences 时,它会起作用</router> - Error: useHref() may be used only in the context of a <Router> component. It works when I directly put the url as localhost:3000/experiences 错误:useRoutes() 只能在<Router>零件 - 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 React js:错误:useLocation()只能在a的上下文中使用<router>成分</router> - React js: Error: useLocation() may be used only in the context of a <Router> component 未捕获的错误:useLocation() 只能在 a 的上下文中使用<router>零件</router> - Uncaught Error: useLocation() may be used only in the context of a <Router> component useNavigate() 只能在<router>零件</router> - useNavigate() may be used only in the context of a <Router> component (反应路由器 v6)“错误:useNavigate() 只能在<router>特定组件中的组件”错误</router> - (react router v6) "Error: useNavigate() may be used only in the context of a <Router> component" error in hoc component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM