Skip to content

Commit

Permalink
[FIX] Unnecessary rooms list render on focus (#1226)
Browse files Browse the repository at this point in the history
  • Loading branch information
djorkaeffalexandre authored and diegolmello committed Sep 24, 2019
1 parent 69839d5 commit f29b9aa
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions app/views/RoomsListView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,19 @@ class RoomsListView extends React.Component {
width
};
Orientation.unlockAllOrientations();
this.willFocusListener = props.navigation.addListener('willFocus', () => {
// Check if there were changes while not focused (it's set on sCU)
if (this.shouldUpdate) {
animateNextTransition();
this.forceUpdate();
this.shouldUpdate = false;
}
});
this.didFocusListener = props.navigation.addListener('didFocus', () => {
BackHandler.addEventListener(
'hardwareBackPress',
this.handleBackPress
);
this.forceUpdate();
});
this.willBlurListener = props.navigation.addListener('willBlur', () => BackHandler.addEventListener(
'hardwareBackPress',
Expand Down Expand Up @@ -204,12 +211,22 @@ class RoomsListView extends React.Component {
}

shouldComponentUpdate(nextProps, nextState) {
const { allChats } = this.state;
// eslint-disable-next-line react/destructuring-assignment
const propsUpdated = shouldUpdateProps.some(key => nextProps[key] !== this.props[key]);
if (propsUpdated) {
return true;
}

// Compare changes only once
const chatsNotEqual = !isEqual(nextState.allChats, allChats);

// If they aren't equal, set to update if focused
if (chatsNotEqual) {
this.shouldUpdate = true;
}

// Abort if it's not focused
if (!nextProps.navigation.isFocused()) {
return false;
}
Expand All @@ -218,7 +235,6 @@ class RoomsListView extends React.Component {
loading,
searching,
width,
allChats,
search
} = this.state;
if (nextState.loading !== loading) {
Expand All @@ -233,7 +249,9 @@ class RoomsListView extends React.Component {
if (!isEqual(nextState.search, search)) {
return true;
}
if (!isEqual(nextState.allChats, allChats)) {
// If it's focused and there are changes, update
if (chatsNotEqual) {
this.shouldUpdate = false;
return true;
}
return false;
Expand Down Expand Up @@ -275,6 +293,9 @@ class RoomsListView extends React.Component {
if (this.querySubscription && this.querySubscription.unsubscribe) {
this.querySubscription.unsubscribe();
}
if (this.willFocusListener && this.willFocusListener.remove) {
this.willFocusListener.remove();
}
if (this.didFocusListener && this.didFocusListener.remove) {
this.didFocusListener.remove();
}
Expand Down

0 comments on commit f29b9aa

Please sign in to comment.