stripe-archive / react-stripe-elements

Moved to stripe/react-stripe-js.
https://github.com/stripe/react-stripe-js
MIT License
3.03k stars 319 forks source link

Documentation: Usage with React-Router and Mobx #473

Closed r3wt closed 4 years ago

r3wt commented 4 years ago

w1zeman1p helped me figure this out in the irc developer support channel. I am providing the info here so it can be verified and potentially incorporated into the documentation.

In order to use with React Router and Mobx:

  1. Route(s) which wish to use react-stripe-elements must be wrapped with StripeProvider, as normal. AsyncStripeProvider will work as well.
  2. The route(s) where react-stripe-elements will be used have to be wrapped in Elements like so:
    <Elements>
    <Route path='/billing/payment-method' exact component={PaymentMethod} />
    </Elements>  
  3. The "Page Component" (eg, PaymentMethod in above example) has to use injectStripe (whether as decorator or middleware like so:

as decorator:

@inject('store') @observer @injectStripe class PaymentMethod extends Component {}

NOTE: the order is important, it has to come after @inject and @observer or it won't work.

as middleware:

class PaymentMethod extends Component {};
export default injectStripe( observer( injectStore( 'store', PaymentMethod )));

version info:

mobx: 5.15.0 mobx-react: 6.1.4 react-router: 5.1.2

mbarrett-stripe commented 4 years ago

Thank you for compiling this information! As of now, this is more specific than we intend to include in this repository's documentation. While I think it is valuable to have this written down, it's not something we want to include in the main docs right now.

This would be well suited for a blog post or article though!

r3wt commented 4 years ago

@mbarrett-stripe doesn't make much sense, because you're including information about redux and redux-connect in your documentation, but whatever you say hombre.