Created
February 1, 2023 23:35
-
-
Save vezaynk/da27c63c3380e59ea0181e1883d03a2a to your computer and use it in GitHub Desktop.
HOC helpers. reduceHOCs and applyHOCs.
This file contains 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