styled-components: "Warning: Unknown props" when wrapping an external component
I have this styled-component:
import FontAwesome from 'react-fontawesome';
export const Icon = styled(FontAwesome)`
font-size: 25px;
margin-bottom: 3px;
${props => props.active && `color: ${props.colors.brand}`}
`;
Which is wrapping FontAwesome which is an external component.
I’m passing active and colors as props but the component complains that they are unknown.

About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 4
- Comments: 24 (13 by maintainers)
@diegohaz thanks for the heads up. I still think it’s a bit strange for this to be the accepted pattern for situations like this.
While we’re waiting for v2 (which will hopefully solve this) I’ve used this approach which works fine and doesn’t pollute the code so much I think. I’ll share it so other can maybe benefit. It used lodash omit to “strip” the custom props (in the example ‘direction’ and ‘align-items’) used by the styled component and then happily send the other ones down the chain.
This is a bit of a bummer. Thanks to @vdanchenkov and @barbalex for your examples on a work around. While it works, it promotes having to ignore
eslint’sno-unused-varsrule. I really dislike having to make exceptions every time I need to leverage this pattern.This is a pretty basic use case when considering
react-router’sLinkcomponent- is there a less hacky way to work around the issue? For anyone curious, here’s another example (which is exactly @barbalex’s example, but using theLinkcomponent):Styled components pass props to the underlying component by design. You would get same results with
Problem is that FontAwesome passes all unknown props to the DOM element.
One solution is to filter props manually
@rjhilgefort you can use http://eslint.org/docs/rules/no-unused-vars#ignorerestsiblings (when it will be fixed https://github.com/eslint/eslint/issues/8119)
oh, just in case other people want to see a complete example (which I missed):
You could also combine both and allow false to pass no props and a object which will whitelist / blacklist (which one should be decided) props.
@k15a
I like it, but most third party components need some custom props. It should be something like this:
We can also use
propTypesto identify the props that belong toIcon, and notFontAwesome.It will not work if one strips
propTypesas part of build process using something likebabel-plugin-transform-react-remove-prop-typesthough.