Skip to content

Instantly share code, notes, and snippets.

@vezaynk
Created February 1, 2023 23:35
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save vezaynk/da27c63c3380e59ea0181e1883d03a2a to your computer and use it in GitHub Desktop.
Save vezaynk/da27c63c3380e59ea0181e1883d03a2a to your computer and use it in GitHub Desktop.
HOC helpers. reduceHOCs and applyHOCs.
interface HOC<T> {
(Component: React.ComponentType<T>): (props: T) => JSX.Element
}
const reduceHOCs = <T>(...hocs: HOC<T>[]): HOC<T> => hocs
.reduce((reduced, next) => (c) => next(reduced(c)));
const applyHOCs = <T>(...hocs: HOC<T>[]) {
const reducedHoc = reduceHOCs(...hocs);
return (Component: React.ComponentType<T>) => {
const WrappedComponent = reducedHoc(Component);
return function (props: T & JSX.IntrinsicAttributes) {
return <WrappedComponent {...props} />;
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment