Skip to content

Instantly share code, notes, and snippets.

@Natumsol
Created August 14, 2019 08:29
Show Gist options
  • Select an option

  • Save Natumsol/eb8d62b308174da0b27470033f669a27 to your computer and use it in GitHub Desktop.

Select an option

Save Natumsol/eb8d62b308174da0b27470033f669a27 to your computer and use it in GitHub Desktop.
withProps
import React from 'react';
// use forwardRef polyfills, preparing for future upgrades to React 16
import forwardRef from 'create-react-ref/lib/forwardRef';
const withProps = (defaultProps = {}) => Base => {
class WithRef extends React.Component {
render() {
const { forwardRef, ...rest } = this.props;
const mergedProps = { ...defaultProps, ...rest };
return (
<Base ref={forwardRef} {...mergedProps} />
);
}
}
return forwardRef((props, ref) => <WithRef {...props} forwardRef={ref} />);
};
export default withProps;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment