# 4.redux的核心 API

# 1.createStore()

  1. 作用:

​ 创建包含指定reducer 的store对象

  1. 编码
import {createStore} from 'redux'
import counter from './reducers/counter'

const store = createStore(counter)
1
2
3
4

# 2.store 对象

    1. 作用:

​ redux 库最核心的管理对象

    1. 它内部维护者:

​ state

​ reducer

    1. 核心方法

​ getState()

​ dispatch(action)

​ subscribe(listener)

    1. 编码
store.getState()
store.dispatch({type: 'increment',number})
store.subscribe(render)
1
2
3

# 3.applyMiddleware()

  1. 作用:

​ 应用于基于 redux 的中间件 (插件库)

​ 安装: npm install --save redux-thunk

  1. 编码:
import {createStore, applyMiddleware} from 'redux'
import thunk from 'redux-thunk'  // redux异步中间件
// 根据counter函数创建store对象
const store = createStore(
	counter,
  	applyMiddleware(thunk) // 应用上异步中间件
)
1
2
3
4
5
6
7

# 4.combineReducers()

  1. 作用:

​ 合并多个 reducer 函数

  1. 编码:
export default combineReducers({
  user,
  chatUser,
  chat
})
1
2
3
4
5
上次更新: 2020/10/27 下午11:58:10