acdlite / redux-router

Redux bindings for React Router – keep your router state inside your Redux store
MIT License
2.3k stars 200 forks source link

History object throws an error when trying to createLocation server.js in beta4. #162

Closed rynbyjn closed 8 years ago

rynbyjn commented 8 years ago

I am using redux-router with isomorphic-fetch server side and ran across an issue if you do not pass a createHistory object into reduxReactRouter({ routes: routes }) in a server.js file in beta4.

This is the error thrown within history:

TypeError: Cannot read property 'apply' of undefined
    at Object.createLocation (/node_modules/history/lib/useQueries.js:99:45)

Before beta4 passing the createHistory property was optional from redux-router/server and createLocation from the requested url just worked. The workaround that I am using is to just call reduxReactRouter({ routes: routes, createHistory: createHistory }) when composing the server store. Note that you can't use createBrowserHistory as it requires a DOM.

Might be nice to auto create a default history object on the server if one does not exist. Another option would be to pass in the location instead of just the url...

Instead of this:

  app.use((req, res) => {
    store.dispatch(match(req.url, (error, redirectLocation, routerState) => { 
      // do your rendering here
    }))
  })

You would use this (this is how react-router expects the location in their match method):

  import createLocation from 'history/lib/createLocation'
  app.use((req, res) => {
    const location = createLocation(req.url)
    store.dispatch(match(location, (error, redirectLocation, routerState) => { 
      // do your rendering here
    }))
  })
twDuke commented 8 years ago

Confused me for several hours. Please document or fix.

xcatliu commented 8 years ago

Related issue: https://github.com/rackt/redux-router/issues/159

xcatliu commented 8 years ago

I followed a server rendering example, then met this issue. Just relate them to help other people. https://github.com/rackt/redux-router/pull/95/files#diff-2762483ac89ccfaf73745a0c7698af36R54

Scarysize commented 8 years ago

The current server-rendering example should clarify what history function to use.

https://github.com/acdlite/redux-router/blob/master/examples/server-rendering/server.js#L11