Dynamic styled(component) by passing component as prop
See original GitHub issueVersion
styled-components: ^2.2.1 babel-plugin-styled-components: ^1.1.7
Steps to reproduce
- Create a styled component that takes a prop containing a component and return it.
const Icon = styled(({ component }) => component)`
background: red;
`
- Use the styled component in your render & pass it a ‘component’ prop.
<Icon component={<MyCustomIco fill={color} width={size} height={size} />} />
- 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:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:@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
inrender
?