# 4.redux的核心 API
# 1.createStore()
- 作用:
创建包含指定reducer 的store对象
- 编码
import {createStore} from 'redux'
import counter from './reducers/counter'
const store = createStore(counter)
1
2
3
4
2
3
4
# 2.store 对象
- 作用:
redux 库最核心的管理对象
- 它内部维护者:
state
reducer
- 核心方法
getState()
dispatch(action)
subscribe(listener)
- 编码
store.getState()
store.dispatch({type: 'increment',number})
store.subscribe(render)
1
2
3
2
3
# 3.applyMiddleware()
- 作用:
应用于基于 redux 的中间件 (插件库)
安装: npm install --save redux-thunk
- 编码:
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
2
3
4
5
6
7
# 4.combineReducers()
- 作用:
合并多个 reducer 函数
- 编码:
export default combineReducers({
user,
chatUser,
chat
})
1
2
3
4
5
2
3
4
5