当前位置:网站首页>Applymiddleware principle
Applymiddleware principle
2022-07-01 10:57:00 【tianruine】
First usage
stay createstore(reducers,applyMiddleware())
In the packaging createstore When it comes to parameters , Made a judgment ,createstore The three parameters of are reducer, preloadedState, enhancer, It could be two , If the second is an object, then the second 2 The parameter is preloadedState, If it is a function, the parameter is enhancer, It's three o'clock , The third parameter is the function, which is enhancer, That is, it can also be written as createstore(reducers,preloadedState,applyMiddleware())
if (
(typeof preloadedState === 'function' && typeof enhancer === 'function') ||
(typeof enhancer === 'function' && typeof arguments[3] === 'function')
) {
throw new Error(
'It looks like you are passing several store enhancers to ' +
'createStore(). This is not supported. Instead, compose them ' +
'together to a single function. See https://redux.js.org/tutorials/fundamentals/part-4-store#creating-a-store-with-enhancers for an example.'
)
}
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. Instead, received: '${kindOf(
enhancer
)}'`
)
}
return enhancer(createStore)(reducer, preloadedState)
}
In terms of parameters , It's passed by formal parameters applyMiddleware() That's the corresponding enhancer, stay applyMiddleware Actually, it was implemented once const store = createStore(...args) That is, you will get store Method of sending , for example getstate,diapatsh, The point is that compose Method this method is used to handle the methods in the middleware ,, for example thuck,saga, First chain There are these in it saga Other methods , And then through compose Deal with these methods , The way to deal with it is , From right to left , The return value of the first function is used as the parameter of the second function , Execute sequentially , And finally it will return a new dispatsh, Be careful :compose The parameter in the second bracket of , That is, the parameters of the last parameter function of the middleware function , yes store.dispatsh And then put store, With the latest dispatsh Back out
applyMiddleware
compose
Pass when there are many parameters reduce Method to merge , Then the final return value is the return result of the first parameter function
bindActionCreators
bindActionCreators yes redux A self-contained function of , The function is to combine single or multiple ActionCreator Turn into dispatch(action) In the form of a set of functions .
Developers don't have to manually dispatch(actionCreator(type)), Instead, you can call methods directly .
The way to do it is , adopt for of Traversal judgment created actionCreators In a folder key Is it a function? Yes, change it by changing this Point to the sending method , Give you automatic execution dispatsh
combineReducers
Its role is to merge when modularizing reducers, That is to say combineReducers(r1,r2) It will be merged into a complete reduce , Then merge this reduce Throw it to createstore
createStore
First return to
dispatch, Used to distribute action, Handle store Inside state Update
subscribe, It is used for monitoring , When state What will you do if you change
getState, It's to get state
replaceReducer, This method can update the current store The use of reducers, You can use this method to insert reducer When the update store,
[$$observable]: observable,
Realization way : First three parameters reducer, preloadedState, enhancer, above applyMiddleware How to judge parameters
stay createStore There's a listeners Arrays are used to exist sbusricb The method of sending monitoring in , There is one getstate yes return This store Of state, The most important way is dispatsh Method , It's in there action Parameters , Executed once reduce And assign the value to state, Then traverse the next listeners All the listening events in the array have just been executed once dispatsh,
There is also a in the source code nextListeners === currentListeners To prevent execution in a build 3 All monitors , In one component unsubscribe, Avoid reporting mistakes
边栏推荐
- The list of winners of the digital collection of "century master" was announced
- [.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
- Half of 2022 has passed, isn't it sudden?
- 12款大家都在用的产品管理平台
- Ask everyone in the group about the fact that the logminer scheme of flick Oracle CDC has been used to run stably in production
- Wireshark TS | 快速重传和乱序之混淆
- 毕业季·进击的技术er
- 云上“视界” 创新无限 | 2022阿里云直播峰会正式上线
- YoDA统一数据应用——融合计算在蚂蚁风险场景下的探索与实践
- [MPC] ① quadratic programming problem matlab solver quadprog
猜你喜欢
【MPC】①二次规划问题MATLAB求解器quadprog
使用强大的DBPack处理分布式事务(PHP使用教程)
Yoda unified data application -- Exploration and practice of fusion computing in ant risk scenarios
106. 从中序与后序遍历序列构造二叉树
我国蜂窝物联网用户已达 15.9 亿,年内有望超越移动电话用户
【AI资讯月刊】350+资源大盘点!6月不容错过的资料和动态,都都都在这里啦!<附下载>
个人商城二开逍遥B2C商城系统源码-可商用版/拼团拼购优惠折扣秒杀源码
移动硬盘驱动器读到,但不显示盘符
The project bar on the left side of CodeBlocks disappears, workspace automatically saves the project, default workspace, open the last workspace, workspace (Graphic tutorial, solved)
PHP有哪些优势和劣势
随机推荐
flutter Uint8List格式的图片和File格式图片的互相转换
The first anniversary of the data security law, which four major changes are coming?
CRC 校验
Can I choose to open an account on CICC securities? Is it safe?
CRC verification
内存泄漏定位工具之 valgrind 使用
. Net 5.0+ does not need to rely on third-party native implementation of scheduled tasks
NC | intestinal cells and lactic acid bacteria work together to prevent Candida infection
[.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
SQL Server列一相同的情况下,如何取列二的最大值,并重新生成表
YoDA统一数据应用——融合计算在蚂蚁风险场景下的探索与实践
想请教一下,我在广州,到哪里开户比较好?现在网上开户安全么?
Database experiment report (II)
106. 从中序与后序遍历序列构造二叉树
数字藏品新一轮热度开启
What are the advantages and disadvantages of PHP
华为HMS Core携手超图为三维GIS注入新动能
CRC 校驗
mysql如何把 一个数据库中的表数据 复制到 另一个数据库中(两个数据库不在同一个数据库链接下)
12. Gateway new generation gateway