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

167
Vistas
How can I make the code shorter when I want to modify a className by each props?

I want to make this code shorter especially at the [if~else if]sentence. because I want to modify a className depends on each props.

export defalut function Button(props) {

const { name, height, color, bgColor } = props;

let className = color + ' ' +bgColor+ ' w-20 rounded border border-black border-solid ';
  
if (height === 'small') {
    className += 'text-xs h-7';
} else if(height === 'medium') {
    className += 'text-base h-11'; 
} else if(height === 'large') {
    className += 'text-lg h-12'; 
}

return (
  <button className={className}>{name}</button>
)}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Use an object indexed by height string.

const classesByHeight = {
  small: 'text-xs h-7',
  medium: 'text-base h-11',
  large: 'text-lg h-12'
};
// ...
const className = `${classesByHeight[height]} ${color} ${bgColor} w-20 rounded border border-black border-solid`;

If the height might not be one of those, then alternate with the empty string too so you don't get an undefined.

const className = `${classesByHeight[height] ?? ''} ${color} ${bgColor} w-20 rounded border border-black border-solid`;
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can use switch statement to replace if else like in the following code:

switch(height) {
  case 'small':
    className += 'text-xs h-7';
    break;
  case 'medium':
    className += 'text-base h-11'; 
    break;
  case 'large':
    className += 'text-lg h-12';
    break;
  default:
    className += '';
} 
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