Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

302
Visualizações
BrowserRouter causing Invalid hook call. Hooks can only be called inside of the body of a function component

I am getting the following error when using react-router-dom version 6:

Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app

It is saying my BrowserRouter is the problem which seems to point to a mismatch of React and the renderer. However, I can't find any issues. Below is the piece of code that is causing the error:

import React from 'react';
import './App.css';
import { BrowserRouter, Route, Routes } from 'react-router-dom';
import Home from "./pages/Home";

function App() {
  return (
    <div>
      <BrowserRouter>
        <Routes>
          <Route path="/" element={Home}/>
        </Routes>
      </BrowserRouter>
    </div>
  );
}

export default App;

Doing a npm ls react yields the below:

├─┬ react-bootstrap@2.0.4
│ ├─┬ @restart/hooks@0.4.5
│ │ └── react@17.0.2 deduped
│ ├─┬ @restart/ui@0.2.5
│ │ ├─┬ @react-aria/ssr@3.1.0
│ │ │ └── react@17.0.2 deduped
│ │ └── react@17.0.2 deduped
│ ├─┬ prop-types-extra@1.1.1
│ │ └── react@17.0.2 deduped
│ ├─┬ react-dom@17.0.2
│ │ └── react@17.0.2 deduped
│ ├─┬ react-transition-group@4.4.2
│ │ └── react@17.0.2 deduped
│ ├── react@17.0.2
│ └─┬ uncontrollable@7.2.1
│   └── react@17.0.2 deduped
├─┬ react-router-dom@6.2.1 extraneous
│ └── react@17.0.2 deduped
└─┬ react-router@6.2.1 extraneous
  └── react@17.0.2 deduped

I'm blocked by this and any help would be greatly appreciated! Thanks!

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I found the issue! I have two package.json files - one in my react app and one outside of it. I had my react-router-dom installed in the wrong package.json. Installing it in the react-app itself fixed the problem.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda