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

167
Visualizações
react-router-dom not working when change path

I am trying to render component Home and About using react-router but I get nothing. I think the problem in the index file with the store. How I can fix it?

Here is a running codesandbox.

index.js

import React from "react";
import reactDom from "react-dom/client";
import App from "./App";
import Store from "./Store";

const root = reactDom.createRoot(document.getElementById("root"));

root.render(
  <Store>
    <App />
  </Store>
);

Store

import React, {createContext, useState} from 'react'
import Layout from './Layout'

export const Data = createContext()


function Store({ children }) {
  const [state, setState] = useState(true)
  const  value = {state, setState}

  return (
    <Data.Provider value={value}>
      <Layout>{children}</Layout>
    </Data.Provider>
  )
}

export default Store

Layout

import React, { useContext } from "react";
import { Data } from "./Store";
import {
  BrowserRouter as Router,
  Route,
  NavLink,
  Routes,
  Link,
} from "react-router-dom";

function Layout({ children }) {
  const { state } = useContext(Data);
  return (
    <div className="layout">
      <Router>
        <div>
        <Link to="/">Home</Link>
          <Link to="/about">About</Link>        </div>
        <div className={`${state ? "hide" : "show"} mobil `}>
          <div className="children">{children} </div>
        </div>
      </Router>
    </div>
  );
}

export default Layout;

App

import React from "react";
import Layout from "./Layout";
import Home from "./Home";
import About from "./About";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

function App() {
  return (
    <Layout>
        <Routes>
          <Route exact path="/" element={<Home />} />
          <Route path="/About" element={<About />} />
        </Routes>
    </Layout>
  );
}

export default App;
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I just happen to see your Layout.js file.

function Layout({ children }) {
  return (
    <div>
      <!-- Router -->
        <Link to="/">Home</Link>
        <Link to="/about">About</Link>
        <div>{children}</div>
      <!-- /Router -->
    </div>
  );
}

You can't use <Router> because the Layout is being recursively used and implicitly you're nesting the <Router> element. It's better to use the element only once in your App before it loads.

about 4 years ago · Juan Pablo Isaza Relatório

0

The store component should receive the children props and pass it down to the Layout, ex:

function Store({children}) {
  const [state, setState] = useState(true)
  const  value = {state, setState}

  return (
    <Data.Provider value={value}>
      <Layout>{children}</Layout>
    </Data.Provider>
  )
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Issue

The code you have shared is rendering a Layout component within another Layout component. The Layout component is rendering a Router. It is an invariant violation to render a router within another router.

function Layout({ children }) {
  const { state } = useContext(Data);
  return (
    <div className="layout">
      <Router> // <-- nested router!!
        <div>
          <Link to="/">Home</Link>
          <Link to="/about">About</Link>{" "}
        </div>
        <div className={`${state ? "hide" : "show"} mobil `}>
          <div className="children">{children} </div>
        </div>
      </Router>
    </div>
  );
}

Both the Store and App components are rendering a Layout component and Store is directly rendering App.

Solution

Remove one or the other Layout component from either Store or App. I suggest removing it from Store so Store is only providing the context value and not mixing in other UI layout and routing context concerns. Store should render the Provider and children prop.

Example:

function Store({ children }) {
  const [state, setState] = useState(true);
  const value = { state, setState };

  return (
    <Data.Provider value={value}>
      {children}
    </Data.Provider>
  );
}

Edit react-router-dom-not-working-whene-change-path

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