Skip to content

Instantly share code, notes, and snippets.

@iremlopsum
Last active December 19, 2020 22:24
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save iremlopsum/6b959e59d17ef0736c30f093b48d155d to your computer and use it in GitHub Desktop.
Save iremlopsum/6b959e59d17ef0736c30f093b48d155d to your computer and use it in GitHub Desktop.
InteractionManager example
import React, { PureComponent } from 'react';
import { InteractionManager } from 'react-native';
class WaitForUI extends PureComponent {
constructor(props) {
super(props);
this.state = {
interactionsComplete: false,
};
}
componentDidMount() {
this.interactionHandle = InteractionManager.runAfterInteractions(() => {
this.setState({ interactionsComplete: true });
this.interactionHandle = null;
});
}
componentWillReceiveProps(nextProps) {
if (nextProps.waitIndex !== this.props.waitIndex) this.handleInteractionManager();
}
componentWillUnmount() {
if (this.interactionHandle) this.interactionHandle.cancel();
}
componentDidUpdate(prevProps, prevState) {
const { onRendered } = this.props;
if (this.state.interactionsComplete && !prevState.interactionsComplete && onRendered) {
onRendered();
}
}
handleInteractionManager() {
if (this.interactionHandle) this.interactionHandle.cancel();
this.setState({ interactionsComplete: false });
this.interactionHandle = InteractionManager.runAfterInteractions(() => {
this.setState({ interactionsComplete: true });
this.interactionHandle = null;
});
}
renderLoader() {
const { loader } = this.props;
if (loader) return loader;
return null
}
render() {
const { interactionsComplete } = this.state;
const { children } = this.props;
if (interactionsComplete && children) return children;
if (!interactionsComplete) return this.renderLoader();
return null;
}
}
export default WaitForUI;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment