Last active
December 7, 2022 13:46
-
-
Save ryerh/f29668b4356233f4899644daaae9e675 to your computer and use it in GitHub Desktop.
multiple react component decorators in typescript
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
import * as React from 'react'; | |
import { withRouter } from 'react-router-dom'; | |
import { RouteComponentProps, Form } from 'antd'; | |
import compose from './compose'; | |
interface DecoratorProps { | |
id: number; | |
} | |
type OwnProps = RouteComponentProps; | |
const finalDecorator = compose<DecoratorProps>( | |
withRouter, | |
Form.create(), | |
); | |
class ApplyForm extends React.Component<OwnProps> { | |
render() {} | |
} | |
export default finalDecorator(ApplyForm); |
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
import * as React from 'react'; | |
// React Component | |
type RC<P> = React.SFC<P> | React.ComponentClass<P>; | |
// High order Component | |
type HOC<P> = (C: RC<P>) => RC<P>; | |
// Compose multiple hocs into single hoc | |
export default function compose<P>(...hocs: Array<HOC<any>>): HOC<P> { | |
return (C: RC<P>) => hocs.reduce((component, hoc) => hoc(component), C); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment