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
then the round property will end up on the div. You can make a custom component that filters out the round from props, but it would be nicer to write e.g.
Properties don't end up on DOM elements unless they are valid.
You can find the code here: StyledComponent.js#L91
You may check out #365 which solves you second problem.
I guess the issue is distinguishing between what should be passed on and what shouldn't, so we need to namespace individual props while avoiding regressions. How about a naming convention based on private class properties having the # prefix?
One more solution. Add styledProps object property and filter it in the wrapper of styled().
// in utils:conststyledWithProps=Comp=>(...parts)=>styled(({styledProps, ...props})=><Comp{...props}/>).apply(null,parts)// in the component:constStyledTest=styledWithProps(Test)` color: ${props=>props.styledProps.isRed ? 'red': 'black'};`;
...
<StyledTesttestProp="test"styledProps={{isRed: true}}/>
Activity
k15a commentedon Jan 26, 2017
Properties don't end up on DOM elements unless they are valid.
You can find the code here: StyledComponent.js#L91
You may check out #365 which solves you second problem.
mxstbr commentedon Jan 26, 2017
We're thinking about how to solve this to get rid of the custom whitelist, actually, just haven't found any nice API yet.
wmertens commentedon Jan 26, 2017
Aha, I couldn't find that issue in my searches.
However, there is still a problem: If a prop is a valid attribute but is not meant as one, it should not be passed. For example
widthandlabel.diegohaz commentedon Jan 27, 2017
@wmertens We've discussed that in #305
The current solution is this:
wmertens commentedon Jan 27, 2017
NathanielHill commentedon Mar 31, 2018
How would one do this when using
withComponent()? Seems to wipe out any filtering in place.For example, if I have this component:
and then I want to modify it slightly, like so:
I will get console errors as
customPropis now passed down to componentNoWorky.I'd rather not have to choose between unfiltered props and code duplication.
holloway commentedon Dec 4, 2018
Is this solved yet in Styled Components 4?
I guess the issue is distinguishing between what should be passed on and what shouldn't, so we need to namespace individual props while avoiding regressions. How about a naming convention based on private class properties having the
#prefix?before
after
anikolaev commentedon Apr 19, 2019
One more solution. Add
styledPropsobject property and filter it in the wrapper ofstyled().Demo: https://codesandbox.io/s/ywl9mxrmk9
Pro: auxiliary code can be moved to separate file, clear syntax, custom props are separated from own props of the styled component.
Cons: if property is not defined there will be error.
lcswillems commentedon Nov 15, 2019
I have just written an RFC for filtering forwarded props: #2878
Would you like a solution like this?
TL;DR It consists of adding a second parameter for filtering props. The current solution:
is turned into:
This is easy to write and to understand, and props filtering (which is not the important part of the definition) is sent at the end of the definition.