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.

Dynamic styled(component) by passing component as prop

See original GitHub issue

Version

styled-components: ^2.2.1 babel-plugin-styled-components: ^1.1.7

Steps to reproduce

  1. Create a styled component that takes a prop containing a component and return it.
const Icon = styled(({ component }) => component)`
  background: red;
`
  1. Use the styled component in your render & pass it a ‘component’ prop.
<Icon component={<MyCustomIco fill={color} width={size} height={size} />} />
  1. The outcome should be that the style is applied to
<MyCustomIco />

Expected Behavior

Created a generic styled component, for example Icon. Then I’m passing a component as a prop to the styled component. In the styled component, I write a function that takes out the component prop (containing my component) and returns it. I expect it to use this component to apply the styled component to.

Actual Behavior

Nothing is being applied to the passed component.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
probablyupcommented, Jan 19, 2018

The styles are generated as a className which needs to be used in the custom ico component. You could modify your thing like this I think:

const Icon = styled(
    ({ component, ...props }) => React.cloneElement(component, props)
)`
  background: red;
`
11reactions
anthonatorcommented, Jul 16, 2018

@kitten can you elaborate on or point me to some documentation explaining why dynamic render components are not recommended? Is it the same issue with using styled in render?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Usage - styled-components
Passing a ref prop to a styled component will give you one of two things depending on the styled target: the underlying DOM...
Read more >
Creating React Styled Components with Dynamic Tags and ...
Props that are passed into styled-components are passed in as attributes of HTML elements. If the component being modified is a React component, ......
Read more >
Make Your CSS Dynamic With Styled Components
styled -components helps you write better CSS in React. It does so by allowing you to wrap all your component's styles into a...
Read more >
How to dynamically pass styles to styled-components
I am trying to make a higher order component with text and an icon that uses a styled-component under the hood for rendering, ......
Read more >
8 awesome features of styled-components - LogRocket Blog
Passing props ​​ styled-components creates a React component, which renders an HTML tag corresponding to the property in the styled object. Button ...
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