Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
react router render component multiple times when refreshing

I'm trying to understand why when refreshing the page, the component is called multiple times:

MainLayout.tsx: (routes component)

import { FC, ReactElement, useEffect } from 'react'
import { Routes, Route, BrowserRouter } from 'react-router-dom'
import { createBrowserHistory } from 'history'
import { useLocation } from 'react-router-dom'
import { Navigate } from 'react-router-dom'
import { Outlet } from 'react-router-dom'
import { IntroductionPage } from '../pages/introduction/introduction-page'
import { useTranslation } from 'react-i18next'
import { storage } from '../utils/storage'

export const history = createBrowserHistory()

export const MainLayout: FC = () => {
const { t } = useTranslation()

useEffect(() => {
  console.log('MainLayout:: constructor')
}, [])

const RequireAuth = (): ReactElement => {
  const { token } = storage.getState().authReducer
  let location = useLocation()
  if (!token) return <Navigate to="/login" state={{ from: location }} />
  return <Outlet />
}

return (
  <BrowserRouter history={history}>
    <div className="main-wrapper">
      <div className="content-wrapper">
        <div className="main-content">
          <Routes>
            <Route path="/" element={<Login />} />
            <Route path="/login" element={<Login />} />
            <Route element={<RequireAuth />}>
              <Route path="/acceptance" element={<AcceptancePage />} />
              <Route path="/introduction/:page" element={<IntroductionPage />} />
            </Route>
          </Routes>
        </div>
      </div>
    </div>
  </BrowserRouter>
)
}

introduction page

I put the following code in introduction page:

useEffect(() => {
console.log('IntroductionPage:: constructor')
setIntroduction(introductions[+page - 1])
}, [])

I'm refreshing the introduction page, and see in the console:

IntroductionPage:: constructor
MainLayout.tsx:23 MainLayout:: constructor
IntroductionPage:: constructor
IntroductionPage:: constructor

Appreciate any help

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Ah, I see why the IntroductionPage component is mounted twice. The RequireAuth component is declared inside another React component. Since it is redeclared each render cycle it's a new React component reference so React unmounts the instance from the previous render cycle and mounts a new instance. All children it renders will also be new instances.

It should be declared out on its own, outside of any other React component.

Example:

const RequireAuth = (): ReactElement => {
  const { token } = storage.getState().authReducer;
  const location = useLocation();
  if (!token) return <Navigate to="/login" state={{ from: location }} />;
  return <Outlet />;
};

export const MainLayout: FC = () => {
  const { t } = useTranslation();

  useEffect(() => {
    console.log('MainLayout:: constructor');
  }, []);

  return (
    <BrowserRouter history={history}>
      <div className="main-wrapper">
        <div className="content-wrapper">
          <div className="main-content">
            <Routes>
              <Route path="/" element={<Login />} />
              <Route path="/login" element={<Login />} />
              <Route element={<RequireAuth />}>
                <Route path="/acceptance" element={<AcceptancePage />} />
                <Route path="/introduction/:page" element={<IntroductionPage />} />
              </Route>
            </Routes>
          </div>
        </div>
      </div>
    </BrowserRouter>
  );
};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda