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

216
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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