mattkrick / redux-optimistic-ui

a reducer enhancer to enable type-agnostic optimistic updates
MIT License
693 stars 36 forks source link

Can't use preloadState in Redux createStore with optimistic reducer and Redux Devtools. #36

Closed marcins closed 7 years ago

marcins commented 7 years ago

Discovered while writing a simple browser test - if you provide preloadState to createStore and you have the Redux Devtools middleware in your store then an error will occur, as for some reason devtools will call the reducer with the raw state not the optimistic state and an @@INIT action. Removing the Devtools middleware from the store makes the error go away.

This is similar to the error I found in #22, except that error was about initialState in the reducer and not preloadState in the store. This error occurs in versions with and without my fix, however adding the @@INIT action to the if statement that checks for @@redux/INIT also makes this error go away, but I'm not sure that's the right thing to do so I'm still looking into it.

marcins commented 7 years ago

I think I found it - the Redux Devtools Instrumentation implements it's own history system (eerily similar to redux-optimistic-ui!), it has an @@INIT action which ends up calling this function with the preloadState value which causes the wrapped reducer to be passed the original state.

I think the options are:

1) add an explicit check for the @@INIT action to the check here: https://github.com/mattkrick/redux-optimistic-ui/blob/master/src/index.js#L97 - which has the effect that the Redux Devtools history expects.

2) replace the init action check with a check that if the state passed in doesn't have a history array & current property then pass it through createState. This should also cover the @@redux/INIT case (and any other middleware out there that might do something weird like this @@INIT action).

I'll add a repro test and work out the best way to fix it.

mattkrick commented 7 years ago

Ahhhhhh that was it! I knew there was a reason I avoided the @@INIT but couldn't for the life of me remember...