question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Passing classes from parent component

See original GitHub issue

Hi

I migrated to Emotion from Glamorous and I miss one thing (or maybe I just don’t know how to do it). It’s connected to this: https://emotion.sh/docs/class-names, but I don’t think it’s actually good developer experience.

What I do is writing self-contained components, which are basically not positioned (no margins, sizes etc) and positioning happens in parent, which layout them.

To achieve that, there are two ways to do this: 1: Wrapper:

  <div className="layout">
    <div className="layout__nav">  
      <NavComponent />
    </div>
  <div />

This more-or-less works, but has some limitations, also required extra markup

2: Mix classes

  <div className="layout">
     <NavComponent className="layout__nav" />
  <div />

This is preferable to me, my component looks like this then:

  const NavComponent = ({...props}) => (<SomeEmotionStyled {...props}>...</SomeEmotionStyled>)

This worked in Glamorous. I’ve seen in <ClassNames> HOC this can be achievable, but it’s a lot of extra code, when I use it quite often.

What is desirable approach here? css function doesn’t return css class, so it’s not easy to pass it down

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Jun 4, 2019

Hopefully, everyone engaged in this conversation has resolved their issue. If not please open a new one.

1reaction
carpbencommented, Jan 31, 2019

I mostly work with Emotion 9, but also work with Emotion 10. Here is an example of what I do :

const Link = styled("a")`
    colour: ${COLORS.GREEN};
    text-decoration: underline;
    ${ ( {stl}) => stl }
`
const BorderedBox = styled("div")`
    border: 1px solid black; 
    ${ ( {stl}) => stl }
`
const Parent = (props) => (
    <BorderedBox stl="margin: 10px;"/>
        Visit us at 
        <Link href="http://www.ourSite.com"  stl=`
            padding: 5px; 
            font-size: 22px; 
        `> 
            Our Cool Website
        </Link>
    </BorderedBox> 
)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why passing a function from a parent Class Component to a ...
I'm trying to pass a method from a parent class component in the props to a child functional component. The parent component basically...
Read more >
How to Pass Props Object from Child Component to Parent ...
Thus, we can see there is no way to pass props from a child component to a parent component. However, we can always...
Read more >
How to pass property from a parent component props to a ...
In this article, we will learn how to pass the property from the parent component to the child component in React js.
Read more >
Pass data or events from a parent component to a child ...
Pass data or event from a child component to parent component in both functional and class components.
Read more >
Pass object to Parent component from Child. I want to pass ...
0 · Know someone who can answer? Share a link to this question via email, Twitter, or Facebook. · Your Answer · Browse...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found