Created
September 1, 2017 23:59
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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