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

154
Visualizações
I am not able to use the router in react js

I have been trying to use a router on react project but getting an error :

A Route is only ever to be used as the child of Routes element, never rendered directly. Please wrap your Route in a Routes.

The Code Is Shown Below

import About from "./container/About";
import Profile from "./container/Profile";
import { useState } from "react";
import {BrowserRouter as Routes,Route} from 'react-router-dom'
function App(){
  const [state, setState] = useState('')
  return(
    <div className="App">
      <button onClick={()=>setState('about')}>About</button>
      <button onClick={()=>setState('profile')}>Profile</button>
      <Routes>
        <Route component={About} path='/about'></Route>
      </Routes>
    </div>
  )
}
export default App;
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I'm not sure you can call the BrowserRouter component the way you did in your code. Perhaps if you wrap your code in the router - in a way that it's the topmost parent, such as this example and render your buttons inside of it then it should work fine.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you're on version 6 of react-router-dom, you need to name the props differently:

<Route element={About} path='/about'></Route>

In version 5, it was named component.

I assume you're on version 6, because I can't remember version 5 having the Routes component. Also, you need to import the components differently, and wrap everything with the BrowserRouter.

import About from "./container/About";
import Profile from "./container/Profile";
import { useState } from "react";
import {BrowserRouter, Routes, Route} from 'react-router-dom'
function App(){
  const [state, setState] = useState('')
  return(
    <BrowserRouter>
      <div className="App">
        <button onClick={()=>setState('about')}>About</button>
        <button onClick={()=>setState('profile')}>Profile</button>
        <Routes>
          <Route element={<About />} path='/about'></Route>
        </Routes>
      </div>
    </BrowserRouter>
  )
}
export default App;
about 4 years ago · Juan Pablo Isaza Relatório

0

Put the path before the component

    <Route path='/about' element={About}></Route>

Also ,

Wrap your in the index.js file with BrowserRouter

<BrowserRouter>
<App/>
</BrowserRouter>
about 4 years ago · Juan Pablo Isaza 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