-
-
Save jpkempf/7ce2f7e3071e869206ea9cea61f2e73f to your computer and use it in GitHub Desktop.
HOC helpers. reduceHOCs and applyHOCs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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