react-router-redux在react-router成为4.0后是不是不需要了

本来在项目打算使用如下的配置,本人通过一些教程中看到了如果想要用react router和redux结合使用的话需要如下的依赖,但是当我从官网的create-creact-app搭建的一个项目中发现了browserHistory这个依赖已经没有了。然后同样的有一个react-router-dom中有一个BrowserRouter这个组件是可以实现相同的,是不是不需要react-router-redux进行router和store同步了啊

import { Router, Route, browserHistory } from 'react-router'
import { syncHistoryWithStore } from 'react-router-redux';
阅读 13.5k
2 个回答

是的。而且在react-router V3也可以不用react-router-redux。这里我说下V3版本如何同步histroystore。不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))

v4需不需要看个人项目需求,在react-router-redux文档中有这么一些说明:

clipboard.png

如果要使用react-router-redux也是可以的,可以参考这个文档说明来使用:

clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题