ReactTraining / react-history

Manage session history with React
266 stars 23 forks source link

Using with redux-saga #12

Closed pshrmn closed 8 years ago

pshrmn commented 8 years ago

Currently, the <History> component expects a createHistory function and a historyOptions object, which it uses to create a history object. Would it be feasible to add a history prop to the component that takes a history object provided by the user?

When it is detected in the setupHistory method, it could be set instead of creating a new history object.

setupHistory() {
  const { history, createHistory, historyOptions } = this.props
  this.history = history ? history : createHistory(historyOptions)
  this.unlisten = this.history.listen(() => this.forceUpdate())
}

There are issues for some users using react-router v4 where they want to do navigation outside of components (e.g., using redux-saga), but because the history object is created within the <History> component, they do not have a good way to access it. This issue would be solved if they could create the history object that would be used by the application.

jfrolich commented 8 years ago

Yes, it would be great if there would be still a way to control the history outside of components

mjackson commented 8 years ago

Using history directly defeats the point of using react-history. If you still need to manipulate the browser's location manually, then there's a problem with our abstraction.

There are issues for some users using react-router v4 where they want to do navigation outside of components

Can you help me understand these use cases? If you need to do navigation manually, why can't you render a <Push> or <Replace>?

mjackson commented 8 years ago

I'd also point your attention to the docs where we show you how you can get access to the original history object if you'd like.

pshrmn commented 8 years ago

This issue was first brought up in ReactTraining/react-router#3972. One solution would be to add the history or router from the context as an extra property of any action that might result in navigation.

I created this issue mostly because adding an extra (and otherwise unrelated to the action) property to actions for navigation feels a bit like littering. Letting the user provide their own history instance which they could reference in the middleware would be a way to avoid that. You can close this issue and move the discussion to the one in react-router if this is not a direction that you want to take.

mjackson commented 8 years ago

Ah, I didn't see you had already opened an issue in the router. Let's keep the discussion in one place then :)