Skip to content

Instantly share code, notes, and snippets.

@treyhoover
Created June 23, 2017 03:25
Show Gist options
  • Save treyhoover/9162329effbfc578756402f0d501110a to your computer and use it in GitHub Desktop.
Save treyhoover/9162329effbfc578756402f0d501110a to your computer and use it in GitHub Desktop.
export default withPassThroughProps({
whiteList: false, /* blacklist these props */
propNames: [...brands, ...sizes],
})(SomeComponent);
import React from "react";
const withPassThroughProps =
({
whiteList,
propNames = [],
as = "passThroughProps"
}) => ComposedComponent => props => {
const input = new Set(propNames);
const keys = Object.keys(props);
const passThroughProps = keys.reduce((obj, key) => {
if (input.has(key) === whiteList) obj[key] = props[key];
return obj;
}, {});
const newProps = { [as]: passThroughProps };
return <ComposedComponent {...props} {...newProps} />
};
export default withPassThroughProps;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment