中间件
redux默认只处理同步,对于API请求这样的异步任务则无能为力。
// constants/ActionTypes.js
export const ADD = 'ADD'
export const SQUARE = 'SQUARE'
export const SET = 'SET'
// action/math.js
import { ADD, SQUARE, SET } from '../constants/ActionTypes'
export const add = num => ({ type: ADD, num })
export const square = { type: SQUARE }
export const setNum = num => ({type: SET,num})
// reduce/math.js
import { ADD, SQUARE,SET } from '../constants/ActionTypes'
const math = (state = 10, action) => {
switch (action.type) {
case ADD:
return state + action.num
case SQUARE:
return state * state
case SET:
return action.num
default:
return state
}
}
export default math
// index.js
import store from './store'
import React from 'react'
import ReactDOM from 'react-dom'
import { add, square, setNum } from './action/math'
import axios from 'axios'
let uri = 'https://jsonplaceholder.typicode.com/posts/2'
const render = () => ReactDOM.render(
<div store={store}>
<p>{store.getState().math}</p>
<input type="button" onClick={() => {axios.get(uri).then(res => {store.dispatch(store.dispatch(setNum(res.data.id)))})}} value="设置Num" />
<input type="button" onClick={() => store.dispatch(add(1))} value="+1" />
<input type="button" onClick={() => store.dispatch(add(2))} value="+2" />
<input type="button" onClick={() => store.dispatch(square)} value="乘方" />
</div>,
document.getElementById('root')
)
render()
store.subscribe(render)
虽然API是异步操作,但store.dispatch
并不是异步,而axios
通过get
方法请求回来数据后,store.dispatch
在axios
中的then
方法中同步取得数据。
【redux-thunk】
如果把异步的请求和非常复杂的逻辑都放在组件里实现,组件会变得臃肿,应利用redux-thunk
。将这些复杂的逻辑放到action
中去处理。redux-thunk
检测到action
是一个函数会进行处理。
首先,使用npm进行安装
npm install --save redux-thunk
然后,使用applyMiddleware
来使用thunk中间件。
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
import rootReducer from '../reducer'
export default createStore(rootReducer,applyMiddleware(thunk))
修改的代码如下:
// action/math.js
import { ADD, SQUARE, SET } from '../action/ActionTypes'
import axios from 'axios'
const uri = 'https://jsonplaceholder.typicode.com/posts/2'
export const add = num => ({ type: ADD, num })
export const square = { type: SQUARE }
export const setNum = () => (dispatch, getState) => {
return axios.get(uri).then(res => {
dispatch({
type: SET,
num: res.data.id
})
})
}
// index.js
import store from './store'
import React from 'react'
import ReactDOM from 'react-dom'
import { add, square, setNum } from './action/math'
const render = () => ReactDOM.render(
<div store={store}>
<p>{store.getState().math}</p>
<input type="button" onClick={() => store.dispatch(setNum())} value="设置Num" />
<input type="button" onClick={() => store.dispatch(add(1))} value="+1" />
<input type="button" onClick={() => store.dispatch(add(2))} value="+2" />
<input type="button" onClick={() => store.dispatch(square)} value="乘方" />
</div>,
document.getElementById('root')
)
render()
store.subscribe(render)
【注意】 redux的中间件,而不是react的。
从上面的图,我们可以看出,中间件在dispatch这一步起作用,仍然返回一个
action对象
,最后交给reducer
处理。
【redux-saga】
超大型项目的异步处理,用redux-saga
。
【redux-persist】
在开发的过程中,数据用redux管理,觉得希望将数据持久化保存,也就是说当用户下一次打开app或网站的时候,我们希望浏览器/APP自动加载出上次的数据。
【redux-immutable】
统一数据格式
常用组件
【react-loadable】
异步组件react-loadable
【withRouter】
只有点击路由时才会加载相应的js
【react-transition-group】
用react-transition-group
实现多个DOM元素的动画