Last active
February 26, 2019 07:47
-
-
Save michal-wrzosek/0cd21cd7701f08f6e6958f4686c9b8e8 to your computer and use it in GitHub Desktop.
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
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>; | |
type Theme = { | |
fontColor: string; | |
}; | |
type WithThemeProps = { | |
theme: Theme; | |
}; | |
const theme = { | |
fontColor: 'red', | |
}; | |
// HOC | |
const withTheme = <P extends object>( | |
Component: React.ComponentType<P>, | |
): React.FC<Omit<P, keyof WithThemeProps>> => props => | |
<Component {...props as P} theme={theme} />; | |
// Component | |
type ListProps = { | |
items: string[]; | |
theme: Theme; | |
}; | |
const List = ({ items, theme }: ListProps) => ( | |
<ul style={{ color: theme.fontColor }}> | |
{items.map((item, index) => ( | |
<li key={index}>{item}</li> | |
))} | |
</ul> | |
); | |
// Wrapping Component in HOC | |
const ListWithTheme = withTheme(List); | |
const App = () => <ListWithTheme items={['one', 'two', 'three']} />; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment