• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

184
Vistas
How to use if statement in export default function in react.js?

Can you explain how to use if statement inside a functional component?

I am trying to define a constant based on the value of an event being passed into props.

For instance,

export default function Event({ evt }) {

//const newVar = if{...} else{...}

//if evt.name is null, set name === 'Not defined"
//else, use evt.name.


return (
       ...my code here that uses newVar
 );
}

Thank you in advance

about 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have a wrong code, try the next example

    evt.name = evt.name ? evt.name : 'Not defined'

if evt.name has a value use the value if not has value user 'not defined' as a default value

Other method to resolve your problem

export default function Event({ evt: { name = 'Not Defined' } }) {
 
return(...)

With this method you are destructuring the object 'evt' and added a default value in name property

about 3 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 Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda