Skip to content

Commit cae48ec

Browse files
committed
Skip styled-components' elements
1 parent d3c3963 commit cae48ec

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/types/element.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ export type MemoElement = {
103103
export type ForwardRefElement = {
104104
type: {
105105
render: ComponentType<DefaultProps> & ComponentStatics,
106-
$$typeof: typeof REACT_FORWARD_REF_TYPE
106+
$$typeof: typeof REACT_FORWARD_REF_TYPE,
107+
styledComponentId?: string // styled-components specific property
107108
},
108109
props: DefaultProps,
109110
$$typeof: typeof REACT_ELEMENT_TYPE

src/visitor.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,18 @@ export const visitElement = (
128128

129129
case REACT_FORWARD_REF_TYPE: {
130130
const refElement = ((element: any): ForwardRefElement)
131-
// Treat inner type as the component instead of React.forwardRef itself
132-
const type = refElement.type.render
133-
const props = refElement.props
134-
const child = mountFunctionComponent(type, props, queue, visitor)
135-
return getChildrenArray(child)
131+
if (typeof refElement.type.styledComponentId === 'string') {
132+
// This is an optimization that's specific to styled-components
133+
// We can safely skip them and return their children
134+
return getChildrenArray(refElement.props.children)
135+
} else {
136+
const {
137+
props,
138+
type: { render }
139+
} = refElement
140+
const child = mountFunctionComponent(render, props, queue, visitor)
141+
return getChildrenArray(child)
142+
}
136143
}
137144

138145
case REACT_ELEMENT_TYPE: {

0 commit comments

Comments
 (0)