Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Don't dispatch action if action would result in no change #222

Closed
mattkrick opened this issue Jan 20, 2016 · 4 comments
Closed

Don't dispatch action if action would result in no change #222

mattkrick opened this issue Jan 20, 2016 · 4 comments

Comments

@mattkrick
Copy link
Contributor

If I'm at /foo and I click a link (eg on the navbar) to go to /foo it currently dispatches an update location. Is this intended? I'd expect the action creator to check if the next location == current location & only then dispatch the action. Otherwise every time a dev writes a reducer built for UPDATE_LOCATION he'll have to run that check himself (as is, it's a bit misleading to say UPDATE when it's technically a MAYBE_UPDATE)

@mjrussell
Copy link

Hmm one concern I would have with this is that I believe React-Router will pass down an updated location as well (with a new key each time if you use hashhistory). You'd want the reducer to always have the same location that React-Router is passing down to its children. I guess if they were deepEqual you could avoid it, but now sure if its worth the perf hit.

Maybe this is a bug/improvement on history itself?

@mattkrick
Copy link
Contributor Author

Not sure I follow... could you give me an example?

@timdorr
Copy link
Member

timdorr commented Jan 20, 2016

Yes, this is intended.

Actions don't make decisions about being dispatched. They don't even have that kind of control. Your reducer is what decides what should happen with an action (ignore it or apply it to state however it wants to).

We could introduce an equality check and swallow duplicate updates. However, that would be an inconsistent behavior that would probably end up being more confusing for users ("It does this like 99% of the time!") You may even want to perform a specific action, such as a page transition, even if you're navigating to the same page.

The reducer we provide can be replaced. It's not hard to extend it, since it's only 3 lines of code. That's the preferable route here. I don't think we need to change anything in the library. If anything, this belongs in history's domain.

@timdorr timdorr closed this as completed Jan 20, 2016
@jlongster
Copy link
Member

This is also very intentional on react-router's part. If you click the same link twice in a browser, it fires events and is considered a new location (better example: click a link to the same location you are on still refreshes the page, it doesn't ignore it).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants