Skip to content

Commit 22b3630

Browse files
authored
fix(connect): Ref on connected component (#65)
Fix access to refs on connected components, after react-redux upgrade @6.0.0
1 parent fb2be07 commit 22b3630

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

src/connect.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable import/no-unresolved, import/extensions */
2-
import React from 'react';
2+
import React, { forwardRef } from 'react';
33
import { connect } from 'react-redux';
44
import hoistStatics from 'hoist-non-react-statics';
55
/* eslint-enable import/no-unresolved, import/extensions */
@@ -39,7 +39,11 @@ const easyConnect = injectProps => (
3939
}
4040
: mapDispatchToProps;
4141

42-
return connect(mapStateToProps, modifiedMapDispatchToProps, ...otherArgs);
42+
return connect(
43+
mapStateToProps,
44+
modifiedMapDispatchToProps,
45+
...otherArgs,
46+
);
4347
};
4448

4549
export default (...args) => (WrappedComponent) => {
@@ -73,30 +77,34 @@ export default (...args) => (WrappedComponent) => {
7377
);
7478
};
7579

76-
getWrappedInstance = () =>
77-
this.innerRef && this.innerRef.getWrappedInstance
78-
? this.innerRef.getWrappedInstance()
79-
: null;
80-
8180
render() {
8281
const { ConnectedComponent } = this;
82+
// eslint-disable-next-line react/prop-types
83+
const { forwardedRef, ...otherProps } = this.props;
8384
const passedProps = {
84-
...this.props,
85+
...otherProps,
8586
...this.state,
86-
ref: (ref) => {
87-
this.innerRef = ref;
88-
},
87+
ref: forwardedRef,
8988
};
9089

91-
return (
92-
<ConnectedComponent
93-
{...passedProps}
94-
/>
95-
);
90+
return <ConnectedComponent {...passedProps} />;
9691
}
9792
}
9893

9994
EasyConnect.displayName = getDisplayName(wrappedComponentName);
95+
EasyConnect.WrappedComponent = WrappedComponent;
96+
97+
if (args[3] && args[3].forwardRef) {
98+
// eslint-disable-next-line react/no-multi-comp
99+
const forwarded = forwardRef((props, ref) => (
100+
<EasyConnect {...props} forwardedRef={ref} />
101+
));
102+
103+
forwarded.displayName = wrappedComponentName;
104+
forwarded.WrappedComponent = WrappedComponent;
105+
106+
return hoistStatics(forwarded, WrappedComponent);
107+
}
100108

101109
return hoistStatics(EasyConnect, WrappedComponent);
102110
};

0 commit comments

Comments
 (0)