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

176
Vistas
Combine dos declaraciones de cambio/caso en el componente React

Estoy creando un componente React (SocialLink) y me pregunto si este es el enfoque mejor/limpio.

 const SocialLink = ({ url, type, title }: Props) => { const renderIcon = () => { let icon; switch (type) { case 'email': icon = <EmailIcon />; break; case 'twitter': icon = <TwitterIcon />; break; case 'facebook': icon = <FacebookIcon />; break; case 'whatsapp': icon = <WhatsAppIcon />; break; default: } return icon; }; const renderUrl = () => { let socialUrl; switch (type) { case 'email': socialUrl = `mailto:?subject=${title} &body=${url}`; break; case 'twitter': socialUrl = `https://twitter.com/share?text=${title}&url=${url}`; break; case 'facebook': socialUrl = `http://www.facebook.com/sharer.php?u=${url}&t=${title}`; break; case 'whatsapp': socialUrl = `whatsapp://send?text=${title} ${url}`; break; default: } return socialUrl; }; return ( <a href={renderUrl()} target="_blank" rel="noopener noreferrer" title={title}> {renderIcon} </a> ); };

Como puede ver, tengo dos declaraciones switch/case . ¿Hay alguna manera inteligente de combinarlos en uno?

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

0

Qué tal si:

 const SocialLink = ({ url, type, title }: Props) => { let icon; let socialUrl; switch (type) { case 'email': icon = <EmailIcon /> socialUrl = `mailto:?subject=${title} &body=${url}`; break; case 'twitter': // etc } return ( <a href={socialUrl} /* ... */> {icon} </a> ) }
about 4 years ago · Juan Pablo Isaza Denunciar

0

Creo que podemos definir un valor constante porque podría ser estático. Será más legible y mantenible.

 const socialLinks = { email: { icon: <EmailIcon />, socialUrl: `mailto:?subject=${title} &body=${url}` }, twitter: { icon: <TwitterIcon />, socialUrl: `https://twitter.com/share?text=${title}&url=${url}` }, .... }; const SocialLink = ({ url, type, title }: Props) => { return ( <a href={socialLinks[type].socialUrl} target="_blank" rel="noopener noreferrer" title={title}> {socialLinks[type].icon} </a> ); };
about 4 years ago · Juan Pablo Isaza Denunciar

0

¿Qué opinas sobre este enfoque?

por supuesto, la variable de datos se puede importar desde una carpeta CONSTANTS o algo así, de esta manera todo quedará claro

 const data = { email: { icon: <EmailIcon />, socialUrl: (title, url) => { return `mailto:?subject=${title} &body=${url}`; } }, twitter: { icon: <TwitterIcon />, socialUrl: (title, url) => { return `https://twitter.com/share?text=${title}&url=${url}`; } }, facebook: { icon: <FacebookIcon />, socialUrl: (title, url) => { return `http://www.facebook.com/sharer.php?u=${url}&t=${title}`; } }, whatsapp: { icon: <WhatsAppIcon />, socialUrl: (title, url) => { return `whatsapp://send?text=${title} ${url}`; } } }; export const SocialLink = ({ url, type, title }) => { return ( <a href={data[type].socialUrl(title, url)} target="_blank" rel="noopener noreferrer" title={title} > {data[type].icon} </a> ); };
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