2 回答
TA贡献2003条经验 获得超2个赞
v4需不需要看个人项目需求,在react-router-redux
文档中有这么一些说明:
如果要使用react-router-redux
也是可以的,可以参考这个文档说明来使用:
TA贡献1874条经验 获得超12个赞
是的。而且在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))
添加回答
举报