Skip to content

Instantly share code, notes, and snippets.

@olecksamdr
Created April 3, 2019 11:43
Show Gist options
  • Save olecksamdr/54900ccaaccd63d3dbe62258f2c5cbd3 to your computer and use it in GitHub Desktop.
Save olecksamdr/54900ccaaccd63d3dbe62258f2c5cbd3 to your computer and use it in GitHub Desktop.
const withProps = input => WrappedComponent => props => (
<WrappedComponent {...props} {...input} />
);
@olecksamdr
Copy link
Author

import _ from 'lodash';
import React from 'react';

const getDisplayName = WrappedComponent => (
  WrappedComponent.displayName || WrappedComponent.name || 'Component'
);

export const withProps = inputProps => (WrappedComponent) => {
  const WithProps = props => (
    <WrappedComponent
      {...props}
      {...(_.isFunction(inputProps) ? inputProps(props) : inputProps)}
    />
  );
  WithProps.displayName = `withProps(${getDisplayName(WrappedComponent)})`;

  return WithProps;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment