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

fix: Only update state if mounted. #1081

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/Pager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ export default class Pager<T extends Route> extends React.Component<
};

componentDidMount() {
this.mounted = true;

// Register this PanGestureHandler with the parent (if parent exists)
// in order to coordinate gestures between handlers.
if (this.context && this.context.addGestureHandlerRef) {
Expand Down Expand Up @@ -232,18 +234,25 @@ export default class Pager<T extends Route> extends React.Component<
}

componentWillUnmount() {
this.mounted = false;

if (this.interactionHandle !== null) {
InteractionManager.clearInteractionHandle(this.interactionHandle);
}
}

mounted = false;

static contextType = PagerContext;

// Mechanism to add child PanGestureHandler refs in the case that this
// Pager is a parent to child Pagers. Allows for coordination between handlers
private providerVal = {
addGestureHandlerRef: (ref: React.RefObject<PanGestureHandler>) => {
if (!this.state.childPanGestureHandlerRefs.includes(ref)) {
if (
!this.state.childPanGestureHandlerRefs.includes(ref) &&
this.mounted
) {
this.setState((prevState: ComponentState) => ({
childPanGestureHandlerRefs: [
...prevState.childPanGestureHandlerRefs,
Expand Down Expand Up @@ -531,7 +540,7 @@ export default class Pager<T extends Route> extends React.Component<
);

private toggleEnabled = () => {
if (this.state.enabled)
if (this.state.enabled && this.mounted)
this.setState({ enabled: false }, () => {
this.setState({ enabled: true });
});
Expand Down Expand Up @@ -577,7 +586,9 @@ export default class Pager<T extends Route> extends React.Component<
// Force componentDidUpdate to fire, whether user does a setState or not
// This allows us to detect when the user drops the update and revert back
// It's necessary to make sure that the state stays in sync
this.forceUpdate();
if (this.mounted) {
this.forceUpdate();
}
}
})
),
Expand Down