Skip to content

Instantly share code, notes, and snippets.

@nim23
Forked from tauqeerkhan12/WaitForUI.js
Created October 24, 2017 04:27
Show Gist options
  • Save nim23/69aee64ef60cd68dd423dad481a5baa1 to your computer and use it in GitHub Desktop.
Save nim23/69aee64ef60cd68dd423dad481a5baa1 to your computer and use it in GitHub Desktop.
InteractionManager example
import React, { PureComponent } from 'react';
import { View, InteractionManager, StyleSheet, ActivityIndicator } from 'react-native';
import { colors } from 'globalStyles';
const styles = StyleSheet.create({
loaderContainer: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
},
})
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 { customLoader } = this.props;
if (customLoader) return customLoader;
return (
<View style={styles.loaderContainer}>
<ActivityIndicator color={colors.blue100} size="large" />
</View>
)
}
render() {
const { interactionsComplete } = this.state;
const { children } = this.props;
if (interactionsComplete && children) return children;
if (!interactionsComplete) return this.renderLoader();
return null;
}
}
export default WaitForUI
WaitForUI.propTypes = {};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment