Created
December 15, 2016 15:10
-
-
Save iamdustan/58088fea51752b8abce0e85e6520a4f3 to your computer and use it in GitHub Desktop.
This file contains 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 scriptjs from 'scriptjs'; | |
const scripts = {}; | |
const withScript = (script, Comp) => { | |
class WithScript extends React.Component { | |
state = { | |
ready: !!scripts[script] | |
}; | |
componentDidMount() { | |
if (!this.state.ready) { | |
scriptsjs.get(script, () => { | |
this.setState({ready: true}); | |
}); | |
} | |
} | |
render () { | |
return <Comp ready={this.state.ready} {...this.props} />; | |
} | |
} | |
}; | |
// Usage | |
const StripeComponent = ( | |
'/path/to/stripe.js', | |
(props) => props.ready ? <CheckoutForm /> : <span>Loading Stripe.js</span> | |
); | |
render(<StripeComponent />, document.body); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment