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 - Best practice to render conditional component

I am quite new to react and like it, but the flexibility gives me some headaches what the best practices are.

For example I have this component, a conditional Login Button.

  const dynamicLogin = () => {
    if (ctx.isLoggedIn) {
      return (
        <button
          className="bg-gray-600 px-8 rounded-lg hover:bg-gray-800"
          onClick={ctx.onLogout}
        >
          Logout
        </button>
      );
    } else {
      return (
        <button
          className="bg-gray-600 px-8 rounded-lg hover:bg-gray-800"
          onClick={ctx.onLogin}
        >
          Login
        </button>
      );
    }
  };

I render it by calling the function is my return statement.

  return (
    <div>
      {ctx.visible ? <Warenkorb></Warenkorb> : ""}

      <nav className="flex justify-between px-4 bg-red-400 text-white text-xl fixed top-0 left-0 w-full">
        <div>
          <h3 className="p-4 inline">
            {ctx.isLoggedIn ? "Eingeloggt!" : "Not Logged In"}
          </h3>
          {dynamicLogin()}
        </div>
        {dynamicWarenkorb()}
...

To be honest, the syntax begins to look quite messi, but it somewhat works. But calling a function inside the return statement feels VERY WRONG. Is the way I render my Components fine or is there a better/faster way to do it. The documentation seems not to be too helpful, because most of the stuff is about class based components :-/.

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

0

I think using components is cleaner than functions in instances like this. For example, you could create an AuthButton component as such:

export function AuthButton({ ctx }) {
  if (ctx.loggedIn) {
    return (
      <button
        className="bg-gray-600 px-8 rounded-lg hover:bg-gray-800"
        onClick={ctx.onLogout}
      >
        Logout
      </button>
    );
  }
  
  return (
    <button
      className="bg-gray-600 px-8 rounded-lg hover:bg-gray-800"
      onClick={ctx.onLogin}
    >
      Login
    </button>
  );
}

Then you can call it in your other component:

import { AuthButton } from './AuthButton';

function OtherComponent() {
  return (
    <div>
      {ctx.visible ? <Warenkorb></Warenkorb> : ""}

      <nav className="flex justify-between px-4 bg-red-400 text-white text-xl fixed top-0 left-0 w-full">
        <div>
          <h3 className="p-4 inline">
            {ctx.isLoggedIn ? "Eingeloggt!" : "Not Logged In"}
          </h3>
          <AuthButton ctx={ctx} />
        </div>
        {dynamicWarenkorb()}
      </nav>
      ...
    </div>
  );
}
about 4 years ago · Juan Pablo Isaza Relatório

0

This is how you can do it, you can place the following piece of code right where you called the function and get rid of the rest

{ctx.isLoggedIn ? (
        <button
          className="bg-gray-600 px-8 rounded-lg hover:bg-gray-800"
          onClick={ctx.onLogout}
        >
          Logout
        </button>
) : (
       <button
          className="bg-gray-600 px-8 rounded-lg hover:bg-gray-800"
          onClick={ctx.onLogin}
        >
          Login
        </button>
)}
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