You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
"Warning: Unknown props" when wrapping an external component #305
I think we will need to get rid of our attributes whitelist anyway one day, so we'll need an API like is being discussed here but that requires a lot more considerations than just solving this one use case.
I'll close this issue for now as I don't think we'll do this in the very near future, but I appreciate all the comments here and we'll definitely use this as a basis for the next discussion about this!
I read the thread, but am still at a loss as to whether something like FontAwesome can be used with styled-components right now. And if yes, is there an example piece of code I can look at?
This is completely erroring up the console in my project as I need to pass many props to influence styling :-(
@vdanchenkov: I don't get how your proposal would work to style a complicated component that has a whole bunch of children: In this case I would have to define the entire jsx outside of the render component and inside the styled function? Or did I get something wrong?
@barbalex My proposal helps to overcome problematic components putting all the props on the root dom element. Like this const Comp = ({props}) => <div {...props}>{props.text}</div>. If you have control over components source code - best way is to not design components this way.
Do not do <div {...props}/> and there will be no need in this workaround.
o.k., got it: the reason @vdanchenkov 's solution did not work for me was:
object spread does not work when stage-0 is preset. I also needed to preset es2015 and install "transform-es2015-destructuring" and "transform-object-rest-spread" plugins
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's no-unused-vars rule. 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's Link component- 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 the Link component):
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.
I think this thread has long reached its limit. The rest of the discussions / suggestions should become - albeit the specific title - part of #439.
There's several proposals on how to solve the whitelist props problem for both HTML and custom components in there, and it would be much appreciated if some more input would go there instead. Cheers!
Activity
vdanchenkov commentedon Dec 10, 2016
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
k15a commentedon Dec 10, 2016
How about something similar to this: #291 (comment)
or
diegohaz commentedon Dec 10, 2016
@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.k15a commentedon Dec 10, 2016
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.
diegohaz commentedon Dec 10, 2016
That
passPropswould be very useful. I currently have some duplicated code with @vdanchenkov's solutione.g. A
Buttonthat renders aMenuButtonfrom react-aria-menubutton or aLinkfrom react-routerIt could be simplified with something like
passProps:diegohaz commentedon Dec 10, 2016
Another issue complaining about it: #135
k15a commentedon Dec 10, 2016
I will probably look into this and #291 tomorrow if nobody else will solve this.
mxstbr commentedon Dec 13, 2016
I think we will need to get rid of our attributes whitelist anyway one day, so we'll need an API like is being discussed here but that requires a lot more considerations than just solving this one use case.
I'll close this issue for now as I don't think we'll do this in the very near future, but I appreciate all the comments here and we'll definitely use this as a basis for the next discussion about this!
aesopwolf commentedon Dec 15, 2016
@mxstbr I know it's only been 2 days since you closed this, but I'd really like to open it up back up.
What do you think about encouraging users to pass a prop called inputProps separately, and apply it instead?
For example:
manolkalinov commentedon Dec 23, 2016
I read the thread, but am still at a loss as to whether something like FontAwesome can be used with styled-components right now. And if yes, is there an example piece of code I can look at?
timmeade commentedon Jan 8, 2017
Just adding my bit to this. Would be very nice to have.
I created a NavItem which is a styled link. I was using a prop to identify to put a left bar on it:
border-left: 1px solid ${props => props.leftBar ? darkBorder : 'transparent'};
but the underlying link is kicking out the unknown props leftBar warning.
I guess I'll just create several NavItem components styled each way to clear the warnings.
barbalex commentedon Jan 18, 2017
This is completely erroring up the console in my project as I need to pass many props to influence styling :-(
@vdanchenkov: I don't get how your proposal would work to style a complicated component that has a whole bunch of children: In this case I would have to define the entire jsx outside of the render component and inside the
styledfunction? Or did I get something wrong?vdanchenkov commentedon Jan 18, 2017
@barbalex My proposal helps to overcome problematic components putting all the props on the root dom element. Like this
const Comp = ({props}) => <div {...props}>{props.text}</div>. If you have control over components source code - best way is to not design components this way.Do not do
<div {...props}/>and there will be no need in this workaround.barbalex commentedon Jan 19, 2017
o.k., got it: the reason @vdanchenkov 's solution did not work for me was:
After making babel happy and uninstalling tcomb it works nicely now.
Thanks all for a great tool and your help!
barbalex commentedon Jan 19, 2017
oh, just in case other people want to see a complete example (which I missed):
rjhilgefort commentedon Feb 7, 2017
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):diegohaz commentedon Feb 7, 2017
@rjhilgefort Try this: https://github.com/bryanrsmith/eslint-plugin-no-unused-vars-rest
You can also use lodash's omit.
rjhilgefort commentedon Feb 7, 2017
@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.
jole78 commentedon Feb 20, 2017
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.
mqklin commentedon Feb 21, 2017
@rjhilgefort you can use http://eslint.org/docs/rules/no-unused-vars#ignorerestsiblings (when it will be fixed eslint/eslint#8119)
timscott commentedon May 2, 2017
Here's a reusable implementation of the workaround offered by @diegohaz and others:
Used like so:
chrisknu commentedon May 3, 2017
@jole78 omit works well and is extensible!
MatthieuLemoine commentedon May 4, 2017
Using Proxy you can do something like that in a
styled.jsfile :And then use
styledinstead ofstyled-components:kitten commentedon May 4, 2017
I think this thread has long reached its limit. The rest of the discussions / suggestions should become - albeit the specific title - part of #439.
There's several proposals on how to solve the whitelist props problem for both HTML and custom components in there, and it would be much appreciated if some more input would go there instead. Cheers!