Skip to content

Instantly share code, notes, and snippets.

@fakenickels
Created September 1, 2017 23:59
Show Gist options
  • Save fakenickels/5d5d645e525666194f81ab6cc7548739 to your computer and use it in GitHub Desktop.
Save fakenickels/5d5d645e525666194f81ab6cc7548739 to your computer and use it in GitHub Desktop.
High order component that shows a waiting component meanwhile a promise does not solve
import React from 'react';
const branchWithResolver = (promise, RightBranch, LeftBranch) => Component =>
class extends React.Component {
displayName = `branchWithResolver(${Component.name})`;
state = { isLoading: true };
componentDidMount = () =>
promise.then(() => this.setState({ isLoading: false }));
render = () =>
this.state.isLoading ? (
<RightBranch {...this.props} />
) : LeftBranch ? (
<RightBranch {...this.props} />
) : (
<Component {...this.props} />
);
};
export default branchWithResolver
/* Usage
function HeavyScene(){}
export default branchWithResolver(cb => InteractionManager.runAfterInteractions(cb), () => <Loading>)(HeavyScene)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment