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

278
Visualizações
ReactJS react-router-dom ^5.3.0 not rendering components

I was using this code without the BrowserRouter component wrapping the Switch and it worked well. When did some jest testing I have got the following error Invariant failed: Browser history needs a DOM which says that the Link component cannot be used outside a router. Indeed, I was not using the router. When I have added the router, the url is changing but components are not redered properly. Did I make it correct?

import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';


export const _routes = [
    {
        component: Home,
        path: '/',
        exact: true
    },
    {
        component: Orders,
        path: '/orders',
        exact: false
    },
    {
        component: OrderConfirm,
        path: '/order-confirm',
        exact: false
    }
]

const App = () => <>
    <Navbar />
    <Router>
        <Switch>
            {_routes.map(route => <Route key={route.path} path={route.path} component={route.component} exact={route.exact} />)}
        </Switch>
    </Router>
</>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This should work as expected :

export const _routes = [
    {
        component: 'Home',
        path: '/',
        exact: true
    },
    {
        component: 'Orders',
        path: '/orders',
        exact: false
    },
    {
        component: 'OrderConfirm',
        path: '/order-confirm',
        exact: false
    }
]



const App = () => <>
    <Navbar />
    <Router>
        <Switch>
            {_routes.map(route => <Route key={route.path} path={route.path} component=require(`./components/${route.component}`).default exact={route.exact} />)}
        </Switch>
    </Router>
</>

I assume that your components are inside components package (this./components/${route.component} shoud be path to your component)

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