Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/Ref on connected component #65

Merged
merged 2 commits into from
Jan 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions src/connect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable import/no-unresolved, import/extensions */
import React from 'react';
import React, { forwardRef } from 'react';
import { connect } from 'react-redux';
import hoistStatics from 'hoist-non-react-statics';
/* eslint-enable import/no-unresolved, import/extensions */
Expand Down Expand Up @@ -39,7 +39,11 @@ const easyConnect = injectProps => (
}
: mapDispatchToProps;

return connect(mapStateToProps, modifiedMapDispatchToProps, ...otherArgs);
return connect(
mapStateToProps,
modifiedMapDispatchToProps,
...otherArgs,
);
};

export default (...args) => (WrappedComponent) => {
Expand Down Expand Up @@ -73,30 +77,34 @@ export default (...args) => (WrappedComponent) => {
);
};

getWrappedInstance = () =>
this.innerRef && this.innerRef.getWrappedInstance
? this.innerRef.getWrappedInstance()
: null;

render() {
const { ConnectedComponent } = this;
// eslint-disable-next-line react/prop-types
const { forwardedRef, ...otherProps } = this.props;
const passedProps = {
...this.props,
...otherProps,
...this.state,
ref: (ref) => {
this.innerRef = ref;
},
ref: forwardedRef,
};

return (
<ConnectedComponent
{...passedProps}
/>
);
return <ConnectedComponent {...passedProps} />;
}
}

EasyConnect.displayName = getDisplayName(wrappedComponentName);
EasyConnect.WrappedComponent = WrappedComponent;

if (args[3] && args[3].forwardRef) {
// eslint-disable-next-line react/no-multi-comp
const forwarded = forwardRef((props, ref) => (
<EasyConnect {...props} forwardedRef={ref} />
));

forwarded.displayName = wrappedComponentName;
forwarded.WrappedComponent = WrappedComponent;

return hoistStatics(forwarded, WrappedComponent);
}

return hoistStatics(EasyConnect, WrappedComponent);
};