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

94
Visualizações
Passing Children Styling Down to a Styled Component

I am making a generalized version of a styled component to replace duplicated code in my codebase. The component looks something like this:

const CustomDiv = styled.div`
  display:flex;
  b {
     padding-right:5px
   }
`;

const DivWrapper = ({style, children}) => {
  return (
    <CustomDiv style={style}>
      {children}
    </CustomDiv>
  );
};

I want to be use this "DivWrapper" in other places of the code base but be able to change the b padding in other places when using it. I tried extending it like this:

const SuperCustomDiv = styled(CustomDiv)`
  b {
    padding-right:10px
  }
`;

I am using the styled components library for React.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can achieve this by using css props and passing props from the styled-component to the CSS. So, we create styled CSS with some property, then this style place to both styled components. With props, we will pass the value to main style.

import styled, { css } from "styled-components";

const PaddingStyle = (value) => css`
  padding-right: ${value}px;
`;

export const SuperCustomDiv = styled.b`
  ${PaddingStyle((props) => props.padding)}
`;

export const CustomDiv = styled.div`
  display: flex;
  b {
    ${PaddingStyle((props) => props.padding)}
  }
`;

export const DivWrapper = ({ padding = 50, children }) => {
  return (
    <CustomDiv as="div" padding={padding}>
      <b>Name:</b> {children}
    </CustomDiv>
  );
};

export default function App() {
  return (
    <div className="App">
      <DivWrapper>John Doe</DivWrapper>
      <SuperCustomDiv padding={30}>Super</SuperCustomDiv>
      <span>John Smith</span>
    </div>
  );
}

Edit dazziling-code

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