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

198
Visualizações
Styled components – accessing props from a parent

I have a component Box that looks like this:

const Box = ({ className, country }) => {
  const status = myStatusFunction(country);
  return (
    <div className={className} status={status}>
      {country}
    </div>
  );
};

I want to use this component in my view, but also add custom styling to it based on the status prop, so my styled.js looks like this:

import _Box from 'components/Box';

const Box = styled(_Box)`
  ${({ status }) => {
    if (status === 'available') {
      return css`
        background-color: green;
      `;
    }
    return css`
      background-color: red;
    `;
  }}
`

The issue here is that the status is undefined here, so my question is – can you somehow access the status prop?

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

0

Since React has a top-down data flow this is not possible. I would suggest to make the status variable a prop of the Box component to follow the top-down data flow.

The meaning of top-down flow is that information only flows from parent to child. By encapsulating the _Box component in the styled function, you basically make the _Box a child component of the newly created styled Box . This means that you in this situation you are trying to gather information of the child in the parent component.

I hope this answers your question :)

about 4 years ago · Juan Pablo Isaza Relatório

0

import { Wrapper } from './styles.js';

const Box = ({ className, country }) => {
  const status = myStatusFunction(country);

  return (
    <Wrapper className={className} status={status}>
      {country}
    </Wrapper>
  );
};

styles.js:


export const Wrapper = styled.div`
  ${({ status }) => {
    if (status === 'available') {
      return css`
        background-color: green;
      `;
    }
    return css`
      background-color: red;
    `;
  }}
`
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