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

197
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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