Skip to content
This repository was archived by the owner on May 17, 2019. It is now read-only.

Commit 1db81dd

Browse files
authored
Avoid setState when unmounted / Fix Flow issues (#146)
* wip * use mounted property
1 parent 027a8a9 commit 1db81dd

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/with-font-loading.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ const withFontLoading = (fontName: string) => {
3030
return (
3131
OriginalComponent: React.ComponentType<*>
3232
): React.ComponentType<*> => {
33-
class WithFontLoading extends React.Component<*> {
33+
class WithFontLoading extends React.Component<*, *> {
3434
props: *;
3535
context: *;
36-
state: *;
36+
state: {$fontStyles: {fontFamily: string}};
37+
mounted: ?boolean;
3738

3839
constructor(props: any, context: any) {
3940
super(props, context);
@@ -48,18 +49,23 @@ const withFontLoading = (fontName: string) => {
4849
}
4950

5051
componentDidMount() {
52+
this.mounted = true;
5153
if (this.state.$fontStyles.fontFamily !== fontName) {
52-
// $FlowFixMe
54+
// $FlowFixMe (this can only be browser so will not be undefined)
5355
loadFont(`${fontName}`).then(() => {
54-
// $FlowFixMe
55-
this.setState({$fontStyles: {fontFamily: fontName}});
56+
this.mounted &&
57+
this.setState({$fontStyles: {fontFamily: fontName}});
5658
});
5759
}
5860
}
5961

6062
render() {
6163
return <OriginalComponent {...{...this.state, ...this.props}} />;
6264
}
65+
66+
componentWillUnmount() {
67+
this.mounted = false;
68+
}
6369
}
6470

6571
WithFontLoading.contextTypes = {

0 commit comments

Comments
 (0)