FormidableLabs / redux-little-router

A tiny router for Redux that lets the URL do the talking.
MIT License
1.04k stars 114 forks source link

Fix missing query on back button #197

Closed tptee closed 7 years ago

tptee commented 7 years ago

Fixes https://github.com/FormidableLabs/redux-little-router/issues/165

Well this sucked...since state (and key) will always be undefined on the first history location, our piggybacking of query and options into state isn't a viable solution anymore (it was hacky anyway).

The tricky part about integrating history is that data you attach to action creators don't necessarily make it to the reducer (history.listen just spits out what the browser gives it). redux-thunk would make this easier, but I don't want to require it or warn people about including it twice.

I solved this with a LIFO queue of navigation actions in the reducer. For example: on PUSH, the reducer stores the payload at the end of the reducer's queue. On LOCATION_CHANGED, the reducer pops off the first payload from the queue and uses its query and options.

The queue works because of these invariants:

Would love if anyone could kick the tires on this in an existing project!

codecov-io commented 7 years ago

Codecov Report

Merging #197 into master will decrease coverage by 0.86%. The diff coverage is 95.45%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master    #197      +/-   ##
=========================================
- Coverage   99.06%   98.2%   -0.87%     
=========================================
  Files          19      18       -1     
  Lines         215     223       +8     
=========================================
+ Hits          213     219       +6     
- Misses          2       4       +2
Impacted Files Coverage Δ
src/types.js 100% <100%> (ø) :arrow_up:
src/environment/browser-router.js 100% <100%> (ø) :arrow_up:
src/actions.js 100% <100%> (ø) :arrow_up:
src/reducer.js 100% <100%> (ø) :arrow_up:
src/enhancer.js 88.88% <75%> (-11.12%) :arrow_down:
src/middleware.js 95.23% <94.73%> (-4.77%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 98ce605...080b3c9. Read the comment docs.