-
-
Notifications
You must be signed in to change notification settings - Fork 15.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redux and routing #805
Comments
Thanks. I've seen that library, but it's not entirely clear to me if it implements something similar to what I'm describing or merely mirrors the location in the state. For example, if something else causes the location in the state to change, will the browser history update and the proper component then be rendered? |
@acdlite Can you answer this? |
This is my approach as well, using react-router in combination with redux-router. It works like this -
As an example, I'm injecting router/URL information like this -
This cover read-only interactions - I get current URL information as part of the application state, and I’m free to reference/inject it as I wish.
React-router and redux-router both use https://github.com/rackt/history for listening to location-related changes. Those hooks plug into native browser events, so anything that changes the location on the browser will also change the application state as a consequence. So you can change the URL manually, or call pushState() from JS completely outside of the React/Redux/Router universe, and the changes will propogate into the react-based state.
This is the second part of the challenge - making “write” changes to the location. As I mentioned, these routing solution simply isten to native brwoser events, so calling those events yourself will be equivalent to making a routing change. Redux-router provides hooks for calling pushState/replaceState in the browser, and it’s possible to add a thin layer of abstraction on top of those hooks to use them as you want. My application behaviour is driven almost entirely by the URL. If I need to change the grouping of items on the page, I’d trigger that by change the URL parameter of groupBy. In my case it made sense to add a small Redux middleware that internally uses redux-router to upate the current url. This way I can throw a simple action like Some sample code might be useful - Redux middleware that provides an action to manipulate the current URL -
I can leverage this middleware by importing the actionCreator it provides, and calling the action any time I need to change the url/query params. For example, a SummaryList component has a grouping selector, and hence provides a onChangeGrouping handler, so I pass a handler that will simply update the current URL
|
I made an example where I explore going one step further: consider the URL wmertens/redux-react-router@3fe33e1 Note that this is just implemented on top of redux-react-router. To make this easy to use, there should be utility functions that help you Using this, you can e.g. make Stack Overflow-type URLs where the question |
@wmertens I believe
As I understand this, to me this objective sounds like two-way binding between the app state and the URL. The current constraint imposed by |
In fact it is working exactly like an input field in redux. A change in The history component should immediately revert the url after a change |
Another cool consequence of binding the URL to the store is that if you |
The more I think about it, the more it seems that just using
So the developer would need to provide a locationToActions ActionCreator |
What's in my opinion fundamentally wrong with
This IMO depends on #569 |
@tomkis1 not fully what you are describing, but does https://github.com/johanneslumpe/redux-history-transitions come closer to the flow you want? |
@tomkis1 Agreed. This is how redux-router can operate, and how my example above also operates. I haven't heard of @johanneslumpe's library when I did the internal implementation, but his interpretation is the same so I'll migrate to use that lib shortly. |
Hi everyone, sorry it's taken me a few days to respond.
Yes, this was one of the original "acceptance criteria" I laid out for the project.
I don't necessarily disagree. I know some other folks have been exploring this area recently. https://github.com/christianalfoni/addressbar treats the address bar like an Redux Router takes its approach to routing from React Router and is best understood as an official integration between those two projects. (For instance, much of the new stuff in the latest release like |
This thread got away from me quickly :) Lots of interesting discussion beyond what I can reply to succinctly, but it's nice to know with a bit more clarify the design goals of redux-router and alternative approaches that people have explored. For my part, I do very much like the idea of treating the browser location as simply another bound component. Thinking in this approach, it is rendered when the state changes (in the simplest case, the location is stored as a string in the state). It and other components can create actions that update the location, a router-like component can render different portions of the app depending on its value, etc. You could imagine a more sophisticated router which can parameterize the location and stuff. |
@johanneslumpe yes, that's basically the approach I would go for |
I'll close this thread. |
Apologies for adding a comment to a closed thread, but I created a small routing solution based on some of the ideas discussed in this thread. You can see it here if you are interested: #637 (comment) |
From what I can tell, react-router seems to be the expected tool to use for routing. I like react-router, but it feels to me that when integrated with redux (at least as implemented in the real world example) it effectively creates a second state which must be reasoned about in addition to the store. That is to say, the store is no longer the driving component of the UI.
Independent of react-router, when thinking about routing in redux, I envisioned a page change being an action, a reducer updating the store with a
location
field, and a store subscriber handling the actual updating of the URL. The UI would then be able to render different components depending on the state oflocation
in the store.Is it already possible to achieve something like this workflow with react-router, possibly with a custom
Link
orhistory
?For that matter, do we even need react-router to achieve this? It seems like it may be possible to isolate some of the interesting parts of react-router to library functions invoked by reducers.
If I'm way off on this please point me in the right direction :)
The text was updated successfully, but these errors were encountered: