/
reducer.js
36 lines (30 loc) · 918 Bytes
/
reducer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { LOCATION_CHANGE } from './actions'
const createConnectRouter = (structure) => {
const {
fromJS,
merge,
} = structure
const createRouterReducer = (history) => {
const initialRouterState = fromJS({
location: history.location,
action: history.action,
})
/*
* This reducer will update the state with the most recent location history
* has transitioned to.
*/
return (state = initialRouterState, { type, payload } = {}) => {
if (type === LOCATION_CHANGE) {
const { location, action, isFirstRendering } = payload
// Don't update the state ref for the first rendering
// to prevent the double-rendering issue on initilization
return isFirstRendering
? state
: merge(state, { location, action })
}
return state
}
}
return createRouterReducer
}
export default createConnectRouter