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

172
Vistas
¿Cómo difundir el objeto de estilos pasado a un componente con estilo como accesorio?

Tengo un componente de texto personalizado que toma una variante obligatoria y accesorios de estilo opcionales:

 <HeaderText variant="h1" style={{ color: 'red' }}>Test</HeaderText>

Luego genero el CSS del componente con estilo en función de la variante, pero también quiero aplicar de alguna manera el estilo opcional personalizado que podría pasar como accesorio. Me imagino que necesitaría esparcirlo en algún lugar, pero no pude averiguar dónde.

 import React, { FunctionComponent } from 'react'; import { TextProps } from 'react-native'; import styled from 'styled-components'; import colors from '../colors'; import fonts from '../fonts'; type Props = { variant?: 'h1' | 'h2' | 'h3'; } & TextProps; const HeaderText: FunctionComponent<Props> = ({ variant = 'h1', style, ...rest }) => { return <StyledText variant={variant} {...rest} />; }; const StyledText = styled.Text` fontFamily: ${(props: any) => styles[props.variant].fontFamily}; color: ${(props: any) => styles[props.variant].color}; fontWeight: ${(props: any) => styles[props.variant].fontWeight}; fontSize: ${(props: any) => styles[props.variant].fontSize}; lineHeight: ${(props: any) => styles[props.variant].lineHeight}; `; const styles = { h1: { fontFamily: fonts.GUARDIAN_EGYP_LIGHT, color: colors.gray900, fontWeight: '300', fontSize: '36px', lineHeight: '40px', }, h2: { fontFamily: fonts.GUARDIAN_EGYP_LIGHT, color: colors.gray900, fontWeight: '300', fontSize: '30px', lineHeight: '39px', }, h3: { fontFamily: fonts.GUARDIAN_EGYP_LIGHT, color: colors.gray900, fontWeight: '300', fontSize: '24px', lineHeight: '34px', } }; export default HeaderText;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Prueba esto. Los componentes con estilo pueden aceptar objetos en lugar de cadenas.

 const styles = { h1: { fontFamily: fonts.GUARDIAN_EGYP_LIGHT, color: colors.gray900, fontWeight: '300', fontSize: '36px', lineHeight: '40px', }, h2: { fontFamily: fonts.GUARDIAN_EGYP_LIGHT, color: colors.gray900, fontWeight: '300', fontSize: '30px', lineHeight: '39px', }, h3: { fontFamily: fonts.GUARDIAN_EGYP_LIGHT, color: colors.gray900, fontWeight: '300', fontSize: '24px', lineHeight: '34px', } }; const Component = styled.div(p => ({ fontFamily: styles[p.variant].fontFamily, color: styles[p.variant].color, fontWeight: styles[p.variant].fontWeight, fontSize: styles[p.variant].fontSize, lineHeight: styles[p.variant].lineHeight })) const App = () => <div> <Component variant="h1">Component</Component> </div>;
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