Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIP-2186 Don't call setState when AbstractTransitionComponent is unmounting #59

Merged
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion lib/src/component/abstract_transition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,16 @@ abstract class AbstractTransitionComponent<T extends AbstractTransitionProps, S
}
}

var _isMounted = false;

@override
void componentDidMount() {
_isMounted = true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this fall back to the antipattern of tracking mounts?

https://facebook.github.io/react/blog/2015/12/16/ismounted-antipattern.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No that is the isMounted method on a React component. They actually recommend this pattern:

"An easy migration strategy for anyone upgrading their code to avoid isMounted() is to track the mounted status yourself. Just set a _isMounted property to true in componentDidMount and set it to false in componentWillUnmount, and use this variable to check your component's status."

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. Just saw it and noticed.

}

@override
void componentWillUnmount() {
_isMounted = false;
_cancelTransitionEventListener();
}

Expand Down Expand Up @@ -333,7 +341,7 @@ abstract class AbstractTransitionComponent<T extends AbstractTransitionProps, S
});
} else {
onNextTransitionEnd(() {
if (state.transitionPhase == TransitionPhase.HIDING) {
if (_isMounted && state.transitionPhase == TransitionPhase.HIDING) {
setState(newState()
..transitionPhase = TransitionPhase.HIDDEN
);
Expand Down
20 changes: 20 additions & 0 deletions test/over_react/component/abstract_transition_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,18 @@ main() {

stopRecordingValidationWarnings();
});

test('does not set hidden state when not mounted', () async {
var renderedInstance = render(Transitioner());
TransitionerComponent transitioner = getDartComponent(renderedInstance);
transitioner.setState(transitioner.newState()..transitionPhase = TransitionPhase.HIDING);

transitioner.handleHiding();
transitioner.componentWillUnmount();
await triggerTransitionEnd(transitioner.getTransitionDomNode());

expect(transitioner.transitionPhasesSet, orderedEquals([TransitionPhase.HIDING]));
});
});
}

Expand Down Expand Up @@ -552,4 +564,12 @@ class TransitionerComponent extends AbstractTransitionComponent<TransitionerProp
props.onHandleHidden();
}
}

List<TransitionPhase> transitionPhasesSet = [];

@override
void setState(dynamic newState, [callback()]) {
super.setState(newState, callback);
transitionPhasesSet.add(newState.transitionPhase);
}
}