isotoma / react-cognito

Library for integrating Facebook React and Amazon Cognito
Apache License 2.0
116 stars 48 forks source link

setupCognito causes state mutation error when passing applyMiddleware to createStore #28

Open HoraceShmorace opened 6 years ago

HoraceShmorace commented 6 years ago

Issue setupCognito doesn't seem to play nicely with Redux's applyMiddleware, resulting in a Redux state mutation error.

Steps to reproduce:

  1. Import applyMiddleware from redux.
  2. Call createStore, passing a call to applyMiddleware (which can be passed any middleware).
  3. Call setupCognito.

Symptom This results in the following error:

Uncaught Error: A state mutation was detected between dispatches, in the path cognito.userPool.client.api.operations.addCustomAttributes.input.defaultValue. This may cause incorrect behavior. (http://redux.js.org/docs/Troubleshooting.html#never-mutate-reducer-arguments) at invariant (browser.js:40) at index.js:53 at Object.dispatch (index.js:14) at setupCognito (react-cognito.js:1191) at index.js:16

The error seems triggered by the call to setupCognito.

HoraceShmorace commented 6 years ago

I'm not sure yet why this is only happening when using applyMiddleware, but I'm pretty sure you shouldn't store a class instance (cognito.userPool) in state since its properties are inherently mutable by its own methods (and thus, mutations can happen outside of a reducer).