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

Commit d2c8be1

Browse files
Merge pull request #77 from stripe/cw-redux
add information about usage with react-redux to the readme
2 parents 466ed5b + 50ebb32 commit d2c8be1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,30 @@ type FactoryProps = {
270270
};
271271
```
272272

273+
## Troubleshooting
274+
275+
`react-stripe-elements` may not work properly when used with components that implement `shouldComponentUpdate`. `react-stripe-elements` relies heavily on React's `context` feature and `shouldComponentUpdate` does not provide a way to take context updates into account when deciding whether to allow a re-render. These components can block context updates from reaching `react-stripe-element` components in the tree.
276+
277+
For example, when using `react-stripe-elements` together with [`react-redux`](https://github.com/reactjs/react-redux) doing the following will not work:
278+
```js
279+
const Component = connect()(injectStripe(_Component));
280+
```
281+
In this case, the context updates originating from the `StripeProvider` are not reaching the components wrapped inside the `connect` function. Therefore, `react-stripe-elements` components deeper in the tree break. The reason is that the `connect` function of `react-redux` [implements `shouldComponentUpdate`](https://github.com/reactjs/react-redux/blob/master/docs/troubleshooting.md#my-views-arent-updating-when-something-changes-outside-of-redux) and blocks re-renders that are triggered by context changes outside of the connected component.
282+
283+
There are two ways to prevent this issue:
284+
285+
1. Change the order of the functions to have `injectStripe` be the outermost one:
286+
```js
287+
const Component = injectStripe(connect()(_CardForm));
288+
```
289+
This works, because `injectStripe` does not implement `shouldComponentUpdate` itself, so context updates originating from the `redux` `Provider` will still reach all components.
290+
291+
2. You can use the [`pure: false`](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) option for `redux-connect`:
292+
```js
293+
const Component = connect(mapStateToProps, mapDispatchToProps, mergeProps, {
294+
pure: false,
295+
})(injectStripe(_CardForm));
296+
```
273297

274298
## Development
275299

0 commit comments

Comments
 (0)