Skip to content

Instantly share code, notes, and snippets.

@bozdoz
Last active April 19, 2021 16:53
Show Gist options
  • Save bozdoz/42563f360179bf2124fdacaa64e6b6ee to your computer and use it in GitHub Desktop.
Save bozdoz/42563f360179bf2124fdacaa64e6b6ee to your computer and use it in GitHub Desktop.
Defining DefaultProps with HOC
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 />