Skip to content

Commit e6b3a49

Browse files
authored
docs(hoc): Use function component as example instead of class (typescript-cheatsheets#221)
1 parent d56b034 commit e6b3a49

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

docs/hoc/full-example.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,21 @@ export function withTheme<T extends WithThemeProps = WithThemeProps>(
6060
WrappedComponent.displayName || WrappedComponent.name || "Component";
6161

6262
// Creating the inner component. The calculated Props type here is the where the magic happens.
63-
return class ComponentWithTheme extends React.Component<
64-
Omit<T, keyof WithThemeProps>
65-
> {
66-
public static displayName = `withTheme(${displayName})`;
67-
68-
public render() {
69-
// Fetch the props you want inject. This could be done with context instead.
70-
const themeProps = getThemePropsFromSomeWhere();
71-
72-
// this.props comes afterwards so the can override the default ones.
73-
return <WrappedComponent {...themeProps} {...(this.props as T)} />;
74-
}
63+
const ComponentWithTheme = (props: Omit<T, keyof WithThemeProps>) => {
64+
// Fetch the props you want to inject. This could be done with context instead.
65+
const themeProps = useTheme();
66+
67+
// props comes afterwards so the can override the default ones.
68+
return <WrappedComponent {...themeProps} {...(props as T)} />;
7569
};
70+
71+
ComponentWithTheme.displayName = `withTheme(${displayName})`;
72+
73+
return ComponentWithTheme;
7674
}
7775
```
7876

79-
Note that the `{...this.props as T}` assertion is needed because of a current bug in TS 3.2 https://github.com/Microsoft/TypeScript/issues/28938#issuecomment-450636046
77+
Note that the `{...(props as T)}` assertion is needed because of a current bug in TS 3.2 https://github.com/Microsoft/TypeScript/issues/28938#issuecomment-450636046
8078

8179
Here is a more advanced example of a dynamic higher order component that bases some of its parameters on the props of the component being passed in:
8280

0 commit comments

Comments
 (0)