[FIX] Unnecessary rooms list render on focus (#1226)
This commit is contained in:
parent
69839d59cd
commit
f29b9aaa75
|
@ -164,12 +164,19 @@ class RoomsListView extends React.Component {
|
||||||
width
|
width
|
||||||
};
|
};
|
||||||
Orientation.unlockAllOrientations();
|
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', () => {
|
this.didFocusListener = props.navigation.addListener('didFocus', () => {
|
||||||
BackHandler.addEventListener(
|
BackHandler.addEventListener(
|
||||||
'hardwareBackPress',
|
'hardwareBackPress',
|
||||||
this.handleBackPress
|
this.handleBackPress
|
||||||
);
|
);
|
||||||
this.forceUpdate();
|
|
||||||
});
|
});
|
||||||
this.willBlurListener = props.navigation.addListener('willBlur', () => BackHandler.addEventListener(
|
this.willBlurListener = props.navigation.addListener('willBlur', () => BackHandler.addEventListener(
|
||||||
'hardwareBackPress',
|
'hardwareBackPress',
|
||||||
|
@ -204,12 +211,22 @@ class RoomsListView extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldComponentUpdate(nextProps, nextState) {
|
shouldComponentUpdate(nextProps, nextState) {
|
||||||
|
const { allChats } = this.state;
|
||||||
// eslint-disable-next-line react/destructuring-assignment
|
// eslint-disable-next-line react/destructuring-assignment
|
||||||
const propsUpdated = shouldUpdateProps.some(key => nextProps[key] !== this.props[key]);
|
const propsUpdated = shouldUpdateProps.some(key => nextProps[key] !== this.props[key]);
|
||||||
if (propsUpdated) {
|
if (propsUpdated) {
|
||||||
return true;
|
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()) {
|
if (!nextProps.navigation.isFocused()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -218,7 +235,6 @@ class RoomsListView extends React.Component {
|
||||||
loading,
|
loading,
|
||||||
searching,
|
searching,
|
||||||
width,
|
width,
|
||||||
allChats,
|
|
||||||
search
|
search
|
||||||
} = this.state;
|
} = this.state;
|
||||||
if (nextState.loading !== loading) {
|
if (nextState.loading !== loading) {
|
||||||
|
@ -233,7 +249,9 @@ class RoomsListView extends React.Component {
|
||||||
if (!isEqual(nextState.search, search)) {
|
if (!isEqual(nextState.search, search)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!isEqual(nextState.allChats, allChats)) {
|
// If it's focused and there are changes, update
|
||||||
|
if (chatsNotEqual) {
|
||||||
|
this.shouldUpdate = false;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -275,6 +293,9 @@ class RoomsListView extends React.Component {
|
||||||
if (this.querySubscription && this.querySubscription.unsubscribe) {
|
if (this.querySubscription && this.querySubscription.unsubscribe) {
|
||||||
this.querySubscription.unsubscribe();
|
this.querySubscription.unsubscribe();
|
||||||
}
|
}
|
||||||
|
if (this.willFocusListener && this.willFocusListener.remove) {
|
||||||
|
this.willFocusListener.remove();
|
||||||
|
}
|
||||||
if (this.didFocusListener && this.didFocusListener.remove) {
|
if (this.didFocusListener && this.didFocusListener.remove) {
|
||||||
this.didFocusListener.remove();
|
this.didFocusListener.remove();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue