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

217
Visualizações
How to use switch-case in react render or return

Currently I am using the ternary operator as shown below. I want to use switch-case for the same logic instead of using ternary operator. How to use it, is it possible to use switch case in return.

return(
    <>
      {
        props.page === 1?
        <h1> Hello </h1>
        :" "

      :
        props.page === 2?
        <h1> Hi </h1>
        :" "
   
      :
        props.page === 3?
        <h1> Good morning </h1>
        :" "

   </>  
 );
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can create a function like message() that takes the props.page as an argument and returns the desired heading.

Like this

function message(page){
  switch(page){
   case 1:
    return "good morning"
   case 2:
    return "good night"
   }
}

function Home(props){
    return (
      <h1>{message(props.page)}</h1>
    )
}

Or you can also return JSX through the Message function.

about 4 years ago · Juan Pablo Isaza Relatório

0

Define switch case in a function and call that function by passing parameters

switchPage(page) {
    switch(page) {
        case 1:
            return 'Hello';
        case 2:
            return 'Hi';
        default:
            return 'Good morning';
    }
}

return (
    <>
        <h1>{this.switchPage(props.page)}</h1>
    </>
);
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