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

Use Promises for ListView onRefresh #232

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion components/ListView.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class ListView extends React.Component {
// TODO(Braco) - add render separator
};

_isMounted = true;

constructor(props, context) {
super(props, context);
this.handleListViewRef = this.handleListViewRef.bind(this);
Expand Down Expand Up @@ -124,6 +126,7 @@ class ListView extends React.Component {
// Reset the global network indicator state
StatusBar.setNetworkActivityIndicatorVisible(false);
}
this._isMounted = false;
}

onRefresh() {
Expand All @@ -132,7 +135,18 @@ class ListView extends React.Component {
});

if (this.props.onRefresh) {
this.props.onRefresh();
const callback = this.props.onRefresh();
const setIdle = () => {
if(this._isMounted) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need this? Is it even possible that onRefresh is called before component is mounted?

Copy link
Author

Choose a reason for hiding this comment

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

It because there is no way to unsubscribe from promises, so this._isMounted check make sure that setState will not execute when component is UnMounted.

this.setState({
status: Status.IDLE,
});
}
};

if(typeof callback === 'object' && typeof callback.then === 'function') {
callback.then(setIdle, setIdle);
}
}
}

Expand Down