2 回答
TA贡献1906条经验 获得超10个赞
是的。而且在react-router
V3也可以不用react-router-redux
。这里我说下V3版本如何同步histroy
到store
。不BB,直接上代码:
import { browserHistory } from 'react-router'
import { createStore } from 'redux'
// action
function locationChange (location = '/') {
return {
type : 'LOCATION_CHANGE',
payload : location
}
}
// reducer
const initialState = browserHistory.getCurrentLocation()
function locationReducer (state = initialState, action) {
return action.type === 'LOCATION_CHANGE'
? action.payload
: state
}
// store
const store = createStore(locationReducer)
const updateLocation = ({ dispatch }) => {
return (nextLocation) => dispatch(locationChange(nextLocation))
}
//绑定browserHistory,取消绑定执行store.unsubscribeHistory()
store.unsubscribeHistory = browserHistory.listen(updateLocation(store))
添加回答
举报