Skip to content

Instantly share code, notes, and snippets.

@michal-wrzosek
Last active February 26, 2019 07:47
Show Gist options
  • Save michal-wrzosek/0cd21cd7701f08f6e6958f4686c9b8e8 to your computer and use it in GitHub Desktop.
Save michal-wrzosek/0cd21cd7701f08f6e6958f4686c9b8e8 to your computer and use it in GitHub Desktop.
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