在学习redux中间件的时候,发现了applyMiddleware写法竟然有两种,但是官方文档只有这一种const store = createStore(reducer, preloadedState, applyMiddleware(...middleware))但是自己在其他地方由看到这种写法const store = applyMiddleware(...middlewares)(createStore)(reducer, initialState)想问下各位这是什么写法,是以前的旧写法还是什么写法?
1 回答
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
这两种写法是等价的,createStore的源码
export default function createStore(reducer, preloadedState, enhancer) { if (typeof preloadedState === 'function' && typeof enhancer === 'undefined') { enhancer = preloadedState preloadedState = undefined } if (typeof enhancer !== 'undefined') { if (typeof enhancer !== 'function') { throw new Error('Expected the enhancer to be a function.') } return enhancer(createStore)(reducer, preloadedState) } ... }
最关键的就是下面这一行
enhancer(createStore)(reducer, preloadedState)
- 1 回答
- 0 关注
- 226 浏览
添加回答
举报
0/150
提交
取消