Skip to content

Commit

Permalink
Fixed set state after component is unmounted
Browse files Browse the repository at this point in the history
  • Loading branch information
marecar3 committed Feb 18, 2019
1 parent 9b94dbe commit 787df73
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import PickerCell from './picker-cell';
class BottomSheet extends Component {
constructor() {
super( ...arguments );
this._isMounted = false;
this.onSafeAreaInsetsUpdate = this.onSafeAreaInsetsUpdate.bind( this );
this.state = {
safeAreaBottomInset: 0,
Expand All @@ -30,17 +31,21 @@ class BottomSheet extends Component {
}

componentDidMount() {
this._isMounted = true;
SafeArea.addEventListener( 'safeAreaInsetsForRootViewDidChange', this.onSafeAreaInsetsUpdate );
}

componentWillUnmount() {
this._isMounted = false;
SafeArea.removeEventListener( 'safeAreaInsetsForRootViewDidChange', this.onSafeAreaInsetsUpdate );
}

onSafeAreaInsetsUpdate( result ) {
const { safeAreaInsets } = result;
if ( this.state.safeAreaBottomInset !== safeAreaInsets.bottom ) {
this.setState( { safeAreaBottomInset: safeAreaInsets.bottom } );
if ( this._isMounted ) {
const { safeAreaInsets } = result;
if ( this.state.safeAreaBottomInset !== safeAreaInsets.bottom ) {
this.setState( { safeAreaBottomInset: safeAreaInsets.bottom } );
}
}
}

Expand Down

0 comments on commit 787df73

Please sign in to comment.