Repository navigation
Conversation
adds support for use of `shouldForwardProp` config method, which can be used to explicitly determine which props should be forwarded on if this config is not provided then existing functionality is preserved (i.e. all props passed to components, or for HTML elements check using `validAttr`) Inspired by the same feature in emotion
|
We have an example where this would be useful in our component library. We're router agnostic, so our The FAQs suggest using an interim component to remove props, however that technique is not compatible with the use of Emotion solves this problem by allowing a With const Link = styled('a').withConfig({
shouldForwardProp: prop => !['fancy', 'colorful'].includes(prop)
})`[styles go here]`and then to use it: <Link as={RouterLink} to="destination" fancy="yes" colorful>Link to destination</Link> |
|
Fixes #439 |
|
Hey @probablyup any chance you could take a look at this? Thanks in advance |
|
Hi, any update on this? This is a very basic feature from emotion missing in styled-components. |
|
Would love to see this ! |
|
As I've said a bunch of times, I'm 👎 on it |
|
@probablyup I'm sorry if I'm OOTL here, but what is the suggestion you propose as an alternative to this? |
|
The only solution that makes sense and works for multiple levels and types of HOC is namespacing in JSX. We can achieve this via babel plugin if someone wants to take a crack at it. |
|
@probablyup please could you link to where this has been discussed previously for those of us not as familiar with all the discussions here? There are 160+ comments on #439 and I don't see anything from you on why this isn't desired - and |
|
for context, we recently moved from emotion to styled components and this is the one thing we miss. I'm not sure what the downside is to adding this to the API, even if another solution could be found in the longer term |
|
#2093 and the constellation of linked tickets in the OP |
|
I can see a lot of discussion around having a convention for prop names/prefixes, but I can't see any argument against also having a filter function as an option in the config. As above, no-one has mentioned shouldForwardProp outside of this PR and #439 so it's hard to find any objections to it! If you're aware of any specific posts/discussion against this that I've missed please let me know. The benefits are:
|
|
I'd also be grateful to see some practical examples of use cases where |
|
It immediately falls apart any time two HOCs could consume the same prop, but potentially want different values or behaviors for it, e.g. "as". |
|
@probablyup That's an intrinsic problem with HOCs, though. I don't think it's relevant for the kinds of problems this PR is trying to solve. |
|
I must admit that I'm struggling to imagine an example of using two HOCs that both want to consume the same prop. My usual expectation when using an HOC is that it will consume the props it is interested in, and not pass those props through. So if I have a component that's wrapped in 2 HOCs I'd typically expect filtering to happen. Besides which, as an HOC author, you're free to write your HOC as you see fit and document it accordingly - if you wanted to leave all props intact then you could set |
|
@stevesims the semantic-ui + styled-components example has come up about a dozen times across all the issues |
Correct and I'd rather fix the problem at the source and introduce an overall pattern that works for all cases rather than implementing layers of hacky patches that don't work combinatorially. |
|
The With this & Those wouldn't be "layers of hacky patches", but direct ways to address specific real world problems. I've yet to see how the proposed "fix at the source" using namespaces would fix these issues in a similarly ergonomic way. |
|
As per I don't think namespaced props would be a desirable resolution to the problem for us. In fact, for our use case, I think I'd consider it more hacky than shouldForwardProp as we'd be putting implementation details in to our prop names (though I'd need to more fully understand the proposed solution and how styled-components would use it). We have a component library that has very few dependencies other than styled-components. We haven't encountered any scenarios where we're not in control of the prop names used by HOCs that we're using. This is because most (maybe all) HOCs we use are ones that we've written ourselves as part of our component library. It's hard to imagine a scenario where we'd encounter this and I would have thought this is the same for many other users of styled-components. I understand your points on how not having namespaced props can be an issue with HOCs in general, but I find it hard to see why this applies as an outright argument against shouldForwardProp. Most of all, I'm in favour of aligning with the emotion API where there isn't a clear performance/maintenance impact and the API extension has clear use cases. I don't see how conflicting HOC prop names means that a shouldForwardProp API extension is hacky. |
|
Ironically, "solving" problems with HOCs: Abstractions just leak |
|
@probablyup You've closed this PR in favor of a proposal in an upstream project that a) probably won't come to fruition b) is not necessarily better as it comes with its own downsides. This seems premature to me. Please consider that while the problem of prop conflicts is pretty rare, the issue of props being passed through to DOM is a common pain point for styled-components users. |
|
A couple more use cases where I think an
|
|
Read through #2093 and checked all the ideas that are there. Currently React throws a whole bunch of (non-critical) errors in my app because custom props are being passed to the DOM level. I've grit my teeth and stuck with it for a while, because I expected an elegant solution to be implemented for what I saw (and still see) to be a relatively small issue with a trivial fix (emotion-style Now that the |
I don't think this PR was actually merged, it has been closed unresolved. So, no, you don't have to go and change your props, but there's also still no official, non-hacky way to solve this. Styled-components is clearly stalled in a limbo of "the perfect solution is the enemy of the good" when it comes to this issue. It's sad. |
|
Namespacing props really isn't a scalable nor a reliable solution here when you're working with an UI library components or design systems which primarily use |
|
The issue seems tied to the nature of HOCs as a whole. What if we ditch them altogether in favor of composition: const Styler = style`
color: red
`
const Button = () => (
<Styler>
<button>click</button>
</Styler>
);The Any thoughts on this pattern? |
|
Is there any movement on this? It would be really good to be able to make a styled component and just pass it the custom props that are needed. Seems like an issue with styled components atm 🤷♂️ |
|
I'm wondering the same as @gcosgreave-us and prob +100 styled-components users. This should indeed be a basic feature that the lib provides. Especially when using styled-system, your DOM gets flooded with props. Thanks. |
|
reopened as #3006 |
Adds support for use of
shouldForwardPropconfig method, which can be used to explicitly determine which props should be forwarded on.If this config is not provided then existing functionality is preserved (i.e. all props passed to components, or for HTML elements check using
validAttr).Inspired by the same feature in emotion.
This feature enables the ability to filter props that would otherwise get passed through to other components, which in turn may get passed through to the DOM.