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.

Can we use styled components export className ?

See original GitHub issue

Consider the following code:

const Wrapper = styled.div`
  &-icon {
    color: #fff;
  }
`
render() {
  const cls = Wrapper.getClassName();
  return <Wrapper>
   <div className={`${cls}-icon`}>xxx</div>
</Wrapper>
}

This saves a lot on the definition of the styled component。

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:20

github_iconTop GitHub Comments

71reactions
mrbonecommented, May 14, 2020

Well, But I want to get the className related to styled.

What for? The nice thing about styled-components is that you don’t have to worry about the specifics of classes all that much.

The above code is same with the following:

That only works as long as they’re all in the same file. Also, attaching the className to the component as a property makes the relationship between the class and the component it belongs to clearer – after all the class won’t work outside of Wrapper.

Please forgive my poor English.

Your English is fine 😃

Same issue, the use case is we need assign a classname to a 3rd UI library to some sub component, eg:

import { css } from 'styled-components';
const generatedCss = css`
  color: red;
`;

const extractFromStyledCss =  generatedCss.getClassName();

<AwesomeSelect dropdownClassname={extractFromStyledCss}>

</AwesomeSelect>
21reactions
martindinescommented, Aug 23, 2020

Hi @d640k

You can convert a styled-component to a string to retrieve it’s CSS Selector:

const Handle = styled.div``
console.log(Handle.toString(), String(Handle), `${Handle}`)

Output:
.sc-clNaTc .sc-clNaTc .sc-clNaTc
Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Usage - styled-components
styled -components has full theming support by exporting a <ThemeProvider> wrapper component. This component provides a theme to all React components underneath ...
Read more >
Pass Styled component as className to another component
Styled components will "wrap" around the base component and try to pass styles to it. Most library components should work with this but...
Read more >
How to Control Class Names in Styled Components
Internally, styled-components generates two classes for each component— one is static and one is dynamic. The class name is generated by ...
Read more >
How To Use React Styled Components Efficiently - Copycat
Using styled-components makes it simple to use actual CSS to style components by writing JavaScript code. These components are known as “styled ...
Read more >
Style library interoperability - Material UI - MUI
Note: If you are using styled-components and have StyleSheetManager with a custom target , make sure that the target is the first element...
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