Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
How to spread styles object passed down to a styled component as a prop?

I have a custom text component that takes a mandatory variant and an optional style props:

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

Then I generate the CSS of the styled component based on the variant but I also want to somehow apply the custom optional styling that might be passed as a prop. I imagine I'd need to spread it somewhere, but I couldn't figure out where.

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 Respostas
Responde à pergunta

0

Try this. Styled components can accept object instead string.

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda