Skip to content

Instantly share code, notes, and snippets.

@jpkempf
Forked from vezaynk/HOC.ts
Created January 30, 2024 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpkempf/7ce2f7e3071e869206ea9cea61f2e73f to your computer and use it in GitHub Desktop.
Save jpkempf/7ce2f7e3071e869206ea9cea61f2e73f 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