redux狀態管理和react-redux的結合使用
原文: https://www.cnblogs.com/kelly2017/p/8034251.html
一:除錯
注意:Redux除錯工具。谷歌中搜redux同理react
新建store的時候判斷window.devToolsExtension
使用compose(組合函式)結合thunk外掛和window.devToolsExtens
二:Redux
Redux 是 JavaScript 狀態容器,專注於狀態管理的庫
整體來說是單一狀態。就是指的是單向的資料流
應用中所有的 state 都儲存在 store 中。 惟一改變 state 的辦法是dispatch觸發 action,為了描述 action 如何改變 state ,需要編寫 reducer。
redux中最重要的幾個:store state action reducer
首先安裝redux 安裝:http://www.cnblogs.com/kelly2017/p/7930681.html
package.json中有
react-redux把狀態對映到子元件 分發reducer
redux 建立reducer action store等
react-thunk thunk處理髮送請求非同步。
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 37 38 |
|
執行結果:
2:Redux和React
redux和react一起結合使用的方法:
1)
因為改變內部 state 惟一方法是 dispatch 一個 action。
所以把store.dispatch的方法傳遞給元件,使其內部可以呼叫修改狀態
react的修改狀態從之前的setState({})變成了由store來統一管理
2)
store.subscribe來監聽每次state修改
每次dispatch都會執行訂閱的事件
subscribe來定義render函式,每次修改都重新渲染頁面。
3)
react和redux的檔案分離
index.js中 建立store
訂閱subscribe只要狀態改變。就重新渲染頁面 render()
const store=createStore(couter)
新建store.以元件屬性的形式。新增到component元件裡
通過subscribe訂閱這個render函式,狀態有變化。render就會重新渲染頁面
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
新建index.redux.js專門管理redux
存放reducer和 dispatch的action //store.dispatch({type:'加'});
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
app.js中使用
內部通過屬性獲取store以及相應的函式
1 |
|
點選的時候要改變狀態 dispatch //addFn() 執行返回的是物件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
執行結果能實現簡單的通過redux管理狀態:
三:redux非同步處理 redux-thunk
強制清除快取 。npm報錯。code為408(不允許安裝) 這時候執行 npm cache clean --force
處理非同步
redux預設情況下只處理同步,想要處理非同步,需要上面安裝的redux-thunk外掛
先引入:
import { createStore ,applyMiddleware } from 'redux';
import thunk from 'redux-thunk'
使用applyMiddleware在建立store的時候開啟中介軟體
const store=createStore(couter ,applyMiddleware(thunk))
之前action是個物件。直接dispatch這個物件就直接修改狀態。store.dispatch({type:MINUS})
export function minusFn() {
return { type:MINUS }
}
action可以返回函式,使用dispatch提交action
addAsyn() 這個函式可以返回個函式。
export function addAsyn() {
return dispatch=>{
setTimeout(()=>{
dispatch(addFn())
},2000)
}
dispatch是個引數。setTimeout結束之後手動執行dispatch
入口:index.js
app.js
index.redux.js
多向外暴露個函式
實現效果:
四:react和redux的結合使用
外掛:react-redux
不適用subscribe釋出事件
提供provider和connect兩個介面連結react和redux
index.js調整
import React from 'react'; import ReactDOM from 'react-dom'; import { createStore ,applyMiddleware } from 'redux'; import thunk from 'redux-thunk' import { Provider } from 'react-redux' import App from './App'; import { couter } from './index.redux'; import registerServiceWorker from './registerServiceWorker'; const store=createStore(couter , applyMiddleware(thunk)) ReactDOM.render( (<Provider store={store}> < App /> </Provider>) , document.getElementById('root')); registerServiceWorker();
app.js中
connect負責從外部獲取元件需要的引數。放到props中
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 |
|
五:@優化
安裝外掛,配置package.json介紹:http://www.cnblogs.com/kelly2017/p/7930681.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
六:多個reducer之間的合併問題
新建一個專門用來合併用的reducer.js 主要的合併的方法 combineReducers
//多個reducer之間的合併 並且返回 import {combineReducers} from 'redux' //redux提供的用於多個reducer合併的方法 // 裡面是個物件。羅列需要合併的reducer import { couter } from './index.redux'; //專案中需要的reducer import { auth } from './Auth.redux'; //專案中需要的reducer export default combineReducers({couter,auth})
在index.js中不需要之前引入的單個的reducer.直接引入合併之後的就可以
1 2 3 4 5 6 |
|
列印結果:合併之後的兩個reducer的初始狀態。