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

168
Visualizações
validate params then navigate with React Router v6

I want to validate the params in my component and do a redirect if they don't pass some logic.

Two things can go wrong:

  1. React gives me a rendering warning:
Warning: Cannot update a component (`BrowserRouter`) while rendering a different component (`Home`).
  1. When refreshing the page directly with the bad param I get the warning:
You should call navigate() in a React.useEffect(), not when your component is first rendered.

I'm not sure I understand why I can't return null, or return any random element from the logic and still have it redirect properly.

I understand it works with useEffect, but I actually want to end execution of the rendering of my component early, to prevent logic further down in the component from running, in the case that the params aren't properly formatted.

See the codesandbox here, (you have to change the url to add an id number): https://codesandbox.io/s/nostalgic-cloud-di0ovf

import "./styles.css";
import {
  BrowserRouter,
  Routes,
  Route,
  Link,
  useNavigate,
  useParams,
} from "react-router-dom";

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/:id" element={<Home />} />
        <Route path="/users" element={<Users />} />
      </Routes>
    </BrowserRouter>
  );
}

function Users() {
  return (
    <div>
      <nav>
        <Link to="/1">Go To 1 (bad param)</Link>
      </nav>
      <h2>Users</h2>
    </div>
  );
}

function Home() {
  const navigate = useNavigate();

  const {id} = useParams();
  
  // check for properly formed params
  if( id < 3){
    navigate({pathname:'/users', replace:true})
    return <span>nothing</span>;
  }

  // other logic that depends on properly formed params
  // don't run this with mal-formed params!

  return (
    <div>
      <nav>
        <Link to="/1">Go To 1 (bad param)</Link>
      </nav>
      <h1>Home: {id}</h1>
    </div>
  );
}
almost 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You should split the logic of issuing the intentional side-effect of navigating from the logic of rendering valid JSX.

Example:

function Home() {
  const navigate = useNavigate();
  const { id } = useParams();

  useEffect(() => {
    if (id < 3) {
      navigate('/users', { replace: true });
    }
  }, [id, navigate]);
  
  // check for properly formed params
  if (id < 3) {
    return <span>nothing</span>; // or null
  }

  // other logic that depends on properly formed params
  // don't run this with mal-formed params!

  return (
    <div>
      <nav>
        <Link to="/1">Go To 1 (bad param)</Link>
      </nav>
      <h1>Home: {id}</h1>
    </div>
  );
}

Alternatively you can render a declarative redirect instead.

Example:

function Home() {
  const { id } = useParams();

  // check for properly formed params
  if (id < 3) {
    return <Navigate to='/users' replace />;
  }

  // other logic that depends on properly formed params
  // don't run this with mal-formed params!

  return (
    <div>
      <nav>
        <Link to="/1">Go To 1 (bad param)</Link>
      </nav>
      <h1>Home: {id}</h1>
    </div>
  );
}
almost 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