Skip to content

Commit a4e4e6c

Browse files
committed
fix: fallback to innerRef when forwardRef is not available. fixes #296
1 parent d59d6ae commit a4e4e6c

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/react/styled.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function styled(tag: React.ComponentType<*> | string) {
2626
}
2727
}
2828

29-
const Result = React.forwardRef((props, ref) => {
29+
const render = (props, ref) => {
3030
const { as: component = tag, class: className, ...rest } = props;
3131

3232
let filteredProps;
@@ -70,20 +70,23 @@ function styled(tag: React.ComponentType<*> | string) {
7070
if (tag.__linaria && tag !== component) {
7171
// If the underlying tag is a styled component, forward the `as` prop
7272
// Otherwise the styles from the underlying component will be ignored
73-
return React.createElement(
74-
tag,
75-
Object.assign(filteredProps, {
76-
as: component,
77-
})
78-
);
73+
filteredProps.as = component;
74+
75+
return React.createElement(tag, filteredProps);
7976
}
8077

8178
return React.createElement(component, filteredProps);
82-
});
79+
};
80+
81+
const Result = React.forwardRef
82+
? React.forwardRef(render)
83+
: // React.forwardRef won't available on older React versions and in Preact
84+
// Fallback to a innerRef prop in that case
85+
({ innerRef, ...rest }) => render(rest, innerRef);
8386

8487
Result.displayName = options.name;
8588

86-
// This properties will be read by the babel plugin for interpolation
89+
// These properties will be read by the babel plugin for interpolation
8790
/* $FlowFixMe */
8891
Result.__linaria = {
8992
className: options.class,

0 commit comments

Comments
 (0)