mattkrick / redux-optimistic-ui

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

Issue with react-router-redux listenForReplays #3

Closed mwalsher closed 8 years ago

mwalsher commented 8 years ago

Hey Matt,

Nice work on this package. I'm having some trouble getting it working with react-router-redux however... specifically the listenForReplays part (see below). Basically the line let routerState = state.getIn(['router', 'location']) evaluates to undefined. Without the optimistic wrapper around rootReducer, everything works fine however. I suspect it has something to do with initialization, but am not sure if it's something I'm doing wrong or if there is an incompatibility somewhere.

const routerMiddleware = syncHistory(history)
  const middleware = [thunkMiddleware, routerMiddleware, apiMiddleware, loggerMiddleware]
  let finalCreateStore = applyMiddleware(...middleware)
  if (__DEBUG__) finalCreateStore = withDevTools(finalCreateStore)

  const store = finalCreateStore(createStore)(optimistic(rootReducer), fromJS(initialState))

  if (__DEBUG__) {
    routerMiddleware.listenForReplays(store, (state) => {
      let routerState = state.getIn(['router', 'location'])
      return routerState.toJS()
    })
  }

Let me know if you need any further information about the configuration.

Cheers

mattkrick commented 8 years ago

see ensureState in the readme. I do something similar in my meatier repo that you can check out.

As a side note, I suggest you keep your 3rd party reducers as pure JS objects. While it's ugly to have a mutable inside an immutable, most developers don't officially support both so you'll eventually end up with at least one that has to be a mutable. Also, all performance gain is lost when you have to convert to an immutable & back to a JS object.

mwalsher commented 8 years ago

Ah yes, of course – thanks!