Skip to content

Instantly share code, notes, and snippets.

@iamdustan
Created December 15, 2016 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamdustan/58088fea51752b8abce0e85e6520a4f3 to your computer and use it in GitHub Desktop.
Save iamdustan/58088fea51752b8abce0e85e6520a4f3 to your computer and use it in GitHub Desktop.
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