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
Styled Components wrapped by an intermediate HOC should not attempt to "fold" and go directly to the underlying Styled Component. The HOC should be included in the tree.
Actual Behavior
When an HOC using hoist-non-react-statics wraps a Styled Component, and then that output is again wrapped in styled(), the static properties from the first Styled Component are observed by styled() and it thinks it's dealing directly with a Styled Component instance, then attempts to "fold" into its styles. This causes the HOC to be skipped over completely – it will never be run – as if styled() were given a hypothetical OutputComponent.WrappedStyledComponent as its target instead.
The completely ideal but impractical solution would be to use the SC-aware version of hoist that knows to skip the internal SC statics. But, for practical purposes, people are already using hoist-non-react-statics everywhere and that's unlikely to change. So here's another idea...
I discussed this with @kitten and proposed a rather simple fix: Styled Component instances could have a static pointer to themselves, like:
constFoo=styled.button``;Foo.styledComponent===Foo;// true// or use a more $internal looking name...
Then when a HOC hoists static properties, completely unaware of SC, the output component would get:
Bar.styledComponent; // Foo
Crucially, the point is that Bar.styledComponent !== Bar. styled() can use this information to realize that it's not actually receiving a Styled Component instance directly, but rather one that has simply had its static properties hoisted. It can skip folding in this case and treat the target like a normal component.
The text was updated successfully, but these errors were encountered:
exogen
changed the title
Styled Component folding causes skipping over HOCs w/ hoisting
Styled Component folding skips over HOCs with hoisting
May 6, 2019
Environment
System:
Binaries:
npmPackages:
Reproduction
https://codesandbox.io/s/vq48p3yox0?fontsize=14
Expected Behavior
Styled Components wrapped by an intermediate HOC should not attempt to "fold" and go directly to the underlying Styled Component. The HOC should be included in the tree.
Actual Behavior
When an HOC using
hoist-non-react-statics
wraps a Styled Component, and then that output is again wrapped instyled()
, the static properties from the first Styled Component are observed bystyled()
and it thinks it's dealing directly with a Styled Component instance, then attempts to "fold" into its styles. This causes the HOC to be skipped over completely – it will never be run – as ifstyled()
were given a hypotheticalOutputComponent.WrappedStyledComponent
as its target instead.The completely ideal but impractical solution would be to use the SC-aware version of
hoist
that knows to skip the internal SC statics. But, for practical purposes, people are already usinghoist-non-react-statics
everywhere and that's unlikely to change. So here's another idea...I discussed this with @kitten and proposed a rather simple fix: Styled Component instances could have a static pointer to themselves, like:
Then when a HOC hoists static properties, completely unaware of SC, the output component would get:
Crucially, the point is that
Bar.styledComponent !== Bar
.styled()
can use this information to realize that it's not actually receiving a Styled Component instance directly, but rather one that has simply had its static properties hoisted. It can skip folding in this case and treat the target like a normal component.The text was updated successfully, but these errors were encountered: