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

130
Vistas
add prop base on condition to react component?
<MyComponent something={somethingIsTrue} />

How to add something as prop to MyComponent if somethingIsTrue? I still wouldn't want something=undefined or something=false because it's still been added to MyComponent. I want it to be

not added as prop

if somethingIsTrue is falsely.

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

0

I have yet to try this but something like this should do the trick.

const conditionalProps = shouldHaveProps ? {something: somethingIsTrue} : {};

<MyComponent {...conditionalProps} />
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can make use of the dynamic expression in JSX for the desired result

<MyComponent {...(somethingIsTrue ? {something:somethingIsTrue} : {})}/>
about 4 years ago · Juan Pablo Isaza Denunciar

0

1) You can do as:

Live Demo

Codesandbox Demo

const something = true;
const props = something ? { something: something } : null;
return <MyComponent {...props} />;

2) You can toggle component with pass argument if something is truthy

Live Demo

Codesandbox Demo

const something = true;

return something 
          ? <MyComponent something={something} /> 
          : <MyComponent />; 
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