Skip to content
This repository was archived by the owner on Nov 27, 2022. It is now read-only.

Commit 4f1982c

Browse files
milesjMiles Johnson
andauthored
fix: Only update state if mounted. (#1081)
### Motivation This component always logs error in the console when testing. ``` Warning: Can't call setState on a component that is not yet mounted. This is a no-op, but it might indicate a bug in your application. Instead, assign to `this.state` directly or define a `state = {};` class property with the desired state in the Pager component. Warning: Can't call forceUpdate on a component that is not yet mounted. This is a no-op, but it might indicate a bug in your application. Instead, assign to `this.state` directly or define a `state = {};` class property with the desired state in the Pager component. ``` PR adds some mounted checks. ### Test plan As long as tests pass and dont log. Co-authored-by: Miles Johnson <miles.johnson@coinbase.com>
1 parent 350f783 commit 4f1982c

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/Pager.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ export default class Pager<T extends Route> extends React.Component<
133133
};
134134

135135
componentDidMount() {
136+
this.mounted = true;
137+
136138
// Register this PanGestureHandler with the parent (if parent exists)
137139
// in order to coordinate gestures between handlers.
138140
if (this.context && this.context.addGestureHandlerRef) {
@@ -232,18 +234,25 @@ export default class Pager<T extends Route> extends React.Component<
232234
}
233235

234236
componentWillUnmount() {
237+
this.mounted = false;
238+
235239
if (this.interactionHandle !== null) {
236240
InteractionManager.clearInteractionHandle(this.interactionHandle);
237241
}
238242
}
239243

244+
mounted = false;
245+
240246
static contextType = PagerContext;
241247

242248
// Mechanism to add child PanGestureHandler refs in the case that this
243249
// Pager is a parent to child Pagers. Allows for coordination between handlers
244250
private providerVal = {
245251
addGestureHandlerRef: (ref: React.RefObject<PanGestureHandler>) => {
246-
if (!this.state.childPanGestureHandlerRefs.includes(ref)) {
252+
if (
253+
!this.state.childPanGestureHandlerRefs.includes(ref) &&
254+
this.mounted
255+
) {
247256
this.setState((prevState: ComponentState) => ({
248257
childPanGestureHandlerRefs: [
249258
...prevState.childPanGestureHandlerRefs,
@@ -531,7 +540,7 @@ export default class Pager<T extends Route> extends React.Component<
531540
);
532541

533542
private toggleEnabled = () => {
534-
if (this.state.enabled)
543+
if (this.state.enabled && this.mounted)
535544
this.setState({ enabled: false }, () => {
536545
this.setState({ enabled: true });
537546
});
@@ -577,7 +586,9 @@ export default class Pager<T extends Route> extends React.Component<
577586
// Force componentDidUpdate to fire, whether user does a setState or not
578587
// This allows us to detect when the user drops the update and revert back
579588
// It's necessary to make sure that the state stays in sync
580-
this.forceUpdate();
589+
if (this.mounted) {
590+
this.forceUpdate();
591+
}
581592
}
582593
})
583594
),

0 commit comments

Comments
 (0)