-
Notifications
You must be signed in to change notification settings - Fork 94
Closed
Description
React 16.3+ forwardRef() will return a new component (type is object) that contains a render function and a $$typeof Symbol, and these should not be copied, otherwise may cause some bug
Example to reproduce
import React, { Component, createRef, forwardRef } from 'react';
import hoistStatics from 'hoist-non-react-statics';
function hoist(WrappedComponent) {
return function createHoistedComponent(TargetComponent) {
const HoistedComponent = forwardRef((props, ref) => (
<TargetComponent {...props} forwardedRef={ref} />
));
if (WrappedComponent) {
HoistedComponent.displayName = `hoisted(${WrappedComponent.displayName})`;
hoistStatics(HoistedComponent, WrappedComponent /* , { render: true } */);
}
return HoistedComponent;
};
}
function extraPropsHoc(extra) {
return (WrappedComponent) => {
@hoist(WrappedComponent)
class Bar extends Component {
render() {
const { forwardedRef, ...props } = this.props;
return <WrappedComponent {...props} {...extra} ref={forwardedRef} />;
}
}
return Bar;
};
}
@extraPropsHoc({ foo: 'foo' })
@extraPropsHoc({ bar: 'bar' })
class Foo extends Component {
static displayName = 'Foo';
log() {
console.log('this.props', this.props);
}
render() {
return <h1>Hello</h1>;
}
}
export default class App extends Component {
ref = createRef();
componentDidMount() {
this.ref.current.log();
}
render() {
return <Foo ref={this.ref} />;
}
}Result
this.props {bar: "bar"}
Expect
this.props {foo: "foo", bar: "bar"}
If I uncomment hoistStatics(HoistedComponent, WrappedComponent /* , { render: true } */), it will work as expected.
Env
klimashkin and kendraknittel
Metadata
Metadata
Assignees
Labels
No labels