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

137
Visualizações
Render a component in jsx if a certain condition is met

Really new to react, I have this part of navigation in my Navbar.js using react-router-dom useLocation hook, I am able to get the active path I have to views and I want to render custom text when a user arrives at the view with JSX something like

if(path==="login" || path==="signin"){
  `<h1>welcome</h1> ${userName}!` 
}

How can I check if condition A or B is met then render the appropriate text using jsx

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You must explicitly tell what to return, in your example you do not return anything. So, You can do either :

if (path === "login" || path === "signin") {
  return (
    <>
      <h1>welcome</h1> {userName}
    </>
  );
} else {
  return null;
}

Or use operator && to conditionally render.

{
  (path === "login" || path === "signin") && (
    <>
      <h1>welcome</h1> {username}
    </>
  );
}

Read more about conditional rendering [here]

about 4 years ago · Juan Pablo Isaza Relatório

0

You need to:

  • Return the value
  • Use JSX and not a string
  • Put everything in a container such as a React Fragment

Such:

if(path==="login" || path==="signin") {
    return <>
        <h1>Welcome</h1>
        {userName}!
    </>;
}

… but that logic should probably be handled by your routes rather than an if statement.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the ternary operator for conditional rendering in JSX like

{path==="login" || path==="signin" ? <> <h1>welcome</h1> {userName} </> : null}

Simple :-)

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