Skip to content
This repository has been archived by the owner on Mar 27, 2021. It is now read-only.

Avoid race condition in async sample code #155

Merged
merged 1 commit into from
Jan 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,14 @@ class App extends React.Component {
this.state = {stripe: null};
}
componentDidMount() {
document.querySelector('#stripe-js').addEventListener('load', () => {
// Create Stripe instance once Stripe.js loads
if (window.Stripe) {
this.setState({stripe: window.Stripe('pk_test_12345')});
});
} else {
document.querySelector('#stripe-js').addEventListener('load', () => {
// Create Stripe instance once Stripe.js loads
this.setState({stripe: window.Stripe('pk_test_12345')});
});
}
}
render() {
// this.state.stripe will either be null or a Stripe instance
Expand All @@ -368,6 +372,12 @@ a Stripe instance. React will re-render `<InjectedCheckoutForm>` when
You can find a working demo of this strategy in [async.js](demo/async/async.js).
If you run the demo locally, you can view it at <http://localhost:8080/async/>.

For alternatives to calling `setState`in `componentDidMount`, consider using a
`setTimeout()`, moving the `if/else` statement to the `constructor`, or
dynamically injecting a script tag in `componentDidMount`. For more
information, see [stripe/react-stripe-elements][issue-154].

[issue-154]: https://github.com/stripe/react-stripe-elements/issues/154


### Server-side rendering (SSR)
Expand Down