亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

揚帆大魚 2018-08-20 17:08:10
本來在項目打算使用如下的配置,本人通過一些教程中看到了如果想要用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';
查看完整描述

2 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

是的。而且在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))


查看完整回答
反對 回復 2018-08-28
  • 2 回答
  • 0 關注
  • 2428 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號