Skip to content

Commit 4a7b515

Browse files
committed
fix: 🐛 fix .setState() warning in <MediaSensor> constructor
1 parent f3e9529 commit 4a7b515

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/MediaSensor/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ export class MediaSensor extends Component<IMediaSensorProps, IMediaSensorState>
7272

7373
this.mql = window.matchMedia(query);
7474

75-
this.setState({
75+
const newState = {
7676
matches: !!this.mql.matches
77-
});
77+
};
78+
79+
if (this.mounted) {
80+
this.setState(newState);
81+
} else {
82+
this.state = newState;
83+
}
7884

7985
this.mql.addListener(this.onMediaChange);
8086
}

0 commit comments

Comments
 (0)