Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

189
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!