Last active
April 19, 2021 16:53
-
-
Save bozdoz/42563f360179bf2124fdacaa64e6b6ee to your computer and use it in GitHub Desktop.
Defining DefaultProps with HOC
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 React from 'react'; | |
const withTheme = <P extends {}>(Comp: React.ComponentType<P>) => { | |
return Comp as any as React.ForwardRefExoticComponent< | |
React.PropsWithoutRef<P> & | |
React.RefAttributes<any> | |
> & { | |
defaultProps: typeof Comp['defaultProps']; | |
}; | |
} | |
class Button extends React.Component<{size: string }> { | |
static defaultProps = { | |
size: 'l' | |
} | |
} | |
const ThemedButton = withTheme(Button) | |
ThemedButton.defaultProps = Button.defaultProps; | |
const a = () => <Button /> | |
const b = () => <ThemedButton /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAJQKYEMDG8BmUIjgcilQ3wG4AoctCAOwGd4B3YGACwBVWkQk4BeOAB4ACnCQAPGEhoATOnADeAXwB8ACgDCuMAC5ExGADot4WtJjsAnmCQiVASn4rF5OG7hEYAVyg04JsDgUeRQaSyD5ZHQjADFoRhQoGWRMAFFxCBhgNACzGhhBV3diqIxDYRwwOgB1FlYILxgUuzgAMiLizv1owxSAQRgYKGAAI0akOkFQyxUOuGdWly63GSRMFC8AGxgKiCq9GGskCEx-bQBtfFX1rZ3KunwAXQpipQolSjRN4PkAIUaYLQxJJpHJumVcjRzIIFHRgAAvJB6BjDGgAczgqiW7gYKCyaDg1w2212VX42K6cMRenwm3wcw+HyotAYcE43CQMn+gyBAmYbHZPDU3MBNHs5EFnJFtEMRNupPkAmlNFla2Jdz2dAozPo8BQ5LUjj4zkEyrgAHpZtRdXARgajSbJVyAUDLUA