{"id":89989,"date":"2020-05-14T11:55:37","date_gmt":"2020-05-14T03:55:37","guid":{"rendered":"http:\/\/4563.org\/?p=89989"},"modified":"2020-05-14T11:55:37","modified_gmt":"2020-05-14T03:55:37","slug":"%e6%b1%82%e6%8c%87%e6%95%99%e4%b8%80%e4%b8%aa-redux-%e7%9b%b8%e5%85%b3%e7%9a%84%e9%97%ae%e9%a2%98%ef%bc%8c%e5%85%88%e8%b0%a2%e8%bf%87%e4%ba%86","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=89989","title":{"rendered":"\u6c42\u6307\u6559\u4e00\u4e2a redux \u76f8\u5173\u7684\u95ee\u9898\uff0c\u5148\u8c22\u8fc7\u4e86"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42\u6307\u6559\u4e00\u4e2a redux \u76f8\u5173\u7684\u95ee\u9898\uff0c\u5148\u8c22\u8fc7\u4e86               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : kimiler <\/span>  <span><i><\/i> 9<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5c24\u5176\u662f\u9762\u5411 c \u7aef\u7684\u7528\u6237\uff0c\u4efb\u4f55\u4e1a\u52a1\u548c\u7528\u6237\u64cd\u4f5c\u6211\u8ba4\u4e3a\u90fd\u5e94\u8be5\u5305\u542b\u8fd9\u56db\u79cd\u72b6\u6001\uff0cloading\uff0cerror\uff0cempty\uff0csuccess\uff0c\u4e3a\u4e86\u7b80\u5316 actionType \u7684\u5206\u6563\u6027\uff0c\u6240\u4ee5\u5c31\u7b80\u5355\u5c01\u88c5\u4e86\u4e00\u4e2a acitonModel<\/p>\n<pre><code>\/**  * generator common actionModel \u666e\u901a\u6a21\u578b  * @param {*} prefix \u524d\u7f00\uff0c\u9700\u8981\u4fdd\u8bc1\u552f\u4e00\u6027  *\/ export function generatorCommonActionModel(prefix) {     const load_loading_action_type = prefix + common_load_loading;     const load_empty_action_type = prefix + common_load_empty;     const load_error_action_type = prefix + common_load_error;     const load_success_action_type = prefix + common_load_success;     const get_common_action_type = prefix + get_common;     const reset_action_type = prefix + common_reset;      return {         prefix,         load_loading_action_type,         load_empty_action_type,         load_error_action_type,         load_success_action_type,         get_common_action_type,         reset_action_type,         loadLoadingActionCreator: params =&gt; ({ type: load_loading_action_type, params }),         loadEmptyActionCreator: params =&gt; ({ type: load_empty_action_type, params }),         loadErrorActionCreator: params =&gt; ({ type: load_error_action_type, params }),         loadSuccessActionCreator: (data, params) =&gt; ({ type: load_success_action_type, payload: { data, params } }),         getCommonActionCreator: params =&gt; ({ type: get_common_action_type, params }),         resetActionCreator: params =&gt; ({ type: reset_action_type, params })     } } <\/code><\/pre>\n<p>actionModel \u6839\u636e key \u7684\u552f\u4e00\u6027\u6765\u533a\u5206\uff0c\u4f7f\u7528\u7684\u65f6\u5019\u76f4\u63a5\u4f7f\u7528\u8be5\u51fd\u6570\u53ef\u4ee5\u5feb\u901f\u751f\u6210\u5bf9\u5e94\u7684 action \u6a21\u578b\uff0c\u5b9e\u9645\u8fd0\u7528\u4e2d\u4e5f\u786e\u5b9e\u611f\u53d7\u5230\u4e86\u5176\u65b9\u4fbf\u6027\uff0c\u4f46\u968f\u4e4b\u5e26\u6765\u7684\u95ee\u9898\u5c31\u6765\u4e86\uff0c\u5c31\u662f reducer \u5c42\uff0c\u59cb\u7ec8\u60f3\u4e0d\u5230\u8be5\u5982\u4f55\u53bb\u4f18\u5316\u5904\u7406\uff0c\u76ee\u524d\u7684\u505a\u6cd5<\/p>\n<pre><code>\/**  * \u7edf\u4e00\u5904\u7406 commonModelActions  * @param {*} actionModel \u9700\u9075\u5faa actionHelp \u89c4\u8303  * @param {*} stateModel  \u9700\u9075\u5faa stateModel \u4e2d\u7684 commonStateModel \u6570\u636e\u6a21\u578b\u89c4\u8303  * @param {*} action dispatch \u51fa\u6765\u7684 action  *\/ export function reducerGeneratorCommonActionModel(actionModel, stateModel, action) {     switch (action.type) {         case actionModel.load_loading_action_type:             return { ...stateModel, load_status: NetWorkComponent.LOAD_LOADING };         case actionModel.load_empty_action_type:             return { ...stateModel, load_status: NetWorkComponent.LOAD_EMPTY };         case actionModel.load_error_action_type:             return { ...stateModel, load_status: NetWorkComponent.LOAD_ERROR };         case actionModel.load_success_action_type:             return {                 ...stateModel,                 load_status: Array.isArray(action.payload.data) ? action.payload.data.length &gt; 0 ? NetWorkComponent.LOAD_SUCCESS : NetWorkComponent.LOAD_EMPTY : NetWorkComponent.LOAD_SUCCESS,                 data: action.payload.data             };         default:             return stateModel;     } }    export default function (state = defaultState, action) {     switch (action.type) {         \/\/\u5f53\u524d\u57ce\u5e02\u5e7f\u544a\u4f4d         case goodShopAdvertisingCommonActionModel.load_loading_action_type:         case goodShopAdvertisingCommonActionModel.load_empty_action_type:         case goodShopAdvertisingCommonActionModel.load_error_action_type:         case goodShopAdvertisingCommonActionModel.load_success_action_type:             return { ...state, advertisings: reducerGeneratorCommonActionModel(goodShopAdvertisingCommonActionModel, state.advertisings, action) }         \/\/\u5f53\u524d\u57ce\u5e02\u7684\u699c\u5355\u5217\u8868         case goodShopRankingShopCommonActionModel.load_loading_action_type:         case goodShopRankingShopCommonActionModel.load_empty_action_type:         case goodShopRankingShopCommonActionModel.load_error_action_type:         case goodShopRankingShopCommonActionModel.load_success_action_type:             return { ...state, rankingShops: reducerGeneratorCommonActionModel(goodShopRankingShopCommonActionModel, state.rankingShops, action) }         \/\/\u5f53\u524d\u57ce\u5e02\u7684\u7206\u54c1\u79d2\u6740\u5546\u54c1\u5217\u8868         case goodShopSeckillCommodityCommonActionModel.load_loading_action_type:         case goodShopSeckillCommodityCommonActionModel.load_empty_action_type:         case goodShopSeckillCommodityCommonActionModel.load_error_action_type:         case goodShopSeckillCommodityCommonActionModel.load_success_action_type:             return { ...state, seckillCommoditys: reducerGeneratorCommonActionModel(goodShopSeckillCommodityCommonActionModel, state.seckillCommoditys, action) }         \/\/\u9644\u8fd1\u7684\u5546\u6237         case goodShopNearbyShopListPagingActionModel.load_loading_action_type:         case goodShopNearbyShopListPagingActionModel.load_empty_action_type:         case goodShopNearbyShopListPagingActionModel.load_error_action_type:         case goodShopNearbyShopListPagingActionModel.load_more_loading_action_type:         case goodShopNearbyShopListPagingActionModel.load_more_empty_action_type:         case goodShopNearbyShopListPagingActionModel.load_more_error_action_type:         case goodShopNearbyShopListPagingActionModel.load_success_action_type:             return { ...state, nearbyShops: reducerGeneratorListPagingActionModel(goodShopNearbyShopListPagingActionModel, state.nearbyShops, action) }         \/\/\u63a8\u8350\u7684\u5546\u6237         case goodShopRecommendShopCommonActionModel.load_loading_action_type:         case goodShopRecommendShopCommonActionModel.load_empty_action_type:         case goodShopRecommendShopCommonActionModel.load_error_action_type:         case goodShopRecommendShopCommonActionModel.load_success_action_type:             return { ...state, recommendShops: reducerGeneratorCommonActionModel(goodShopRecommendShopCommonActionModel, state.recommendShops, action) }         \/\/\u4eba\u6c14\u5546\u5708\u7684\u5546\u6237         case goodShopHotShopListPagingActionModel.load_loading_action_type:         case goodShopHotShopListPagingActionModel.load_empty_action_type:         case goodShopHotShopListPagingActionModel.load_error_action_type:         case goodShopHotShopListPagingActionModel.load_more_loading_action_type:         case goodShopHotShopListPagingActionModel.load_more_empty_action_type:         case goodShopHotShopListPagingActionModel.load_more_error_action_type:         case goodShopHotShopListPagingActionModel.load_success_action_type:             return { ...state, hotShops: reducerGeneratorListPagingActionModel(goodShopHotShopListPagingActionModel, state.hotShops, action) };         \/\/\u4eba\u6c14\u5546\u5708\u680f\u76ee\u5206\u7c7b         case goodShopHotShopClassifyCommonActionModel.load_loading_action_type:         case goodShopHotShopClassifyCommonActionModel.load_empty_action_type:         case goodShopHotShopClassifyCommonActionModel.load_error_action_type:         case goodShopHotShopClassifyCommonActionModel.load_success_action_type:             return { ...state, hotShopsClassify: reducerGeneratorCommonActionModel(goodShopHotShopClassifyCommonActionModel, state.hotShopsClassify, action) };         \/\/\u699c\u5355\u8be6\u60c5         case goodShopRankingShopDetailCommonActionModel.load_loading_action_type:         case goodShopRankingShopDetailCommonActionModel.load_empty_action_type:         case goodShopRankingShopDetailCommonActionModel.load_error_action_type:         case goodShopRankingShopDetailCommonActionModel.load_success_action_type:             return { ...state, rankingDetails: reducerMergeGeneratorCommonActionModel(goodShopRankingShopDetailCommonActionModel, state.rankingDetails, action) }         \/\/\u652f\u6301\u7684\u57ce\u5e02\u5217\u8868         case goodShopCitysCommonActionModel.load_loading_action_type:         case goodShopCitysCommonActionModel.load_empty_action_type:         case goodShopCitysCommonActionModel.load_error_action_type:         case goodShopCitysCommonActionModel.load_success_action_type:             return { ...state, citys: reducerGeneratorCommonActionModel(goodShopCitysCommonActionModel,  <\/code><\/pre>\n<p>\u611f\u89c9\u8fd9\u6837\u53bb\u5199\uff0c\u53cd\u800c\u66f4\u7d2f\u4e86\uff0c\u6240\u4ee5\u60f3\u95ee\u4e0b v \u53cb\u95e8\uff0c\u8fd9\u5757\u8981\u5982\u4f55\u53bb\u8bbe\u8ba1\u548c\u4f18\u5316\u554a\uff0c\u518d\u6b21\u611f\u8c22<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1331991\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : jigi330 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4ee3\u7801\u592a\u957f\u4e0d\u60f3\u770b\u4e86\uff0c\u611f\u89c9\u662f\u6839\u636e action \u7684\u4e0d\u540c\u7684 type \u53bb\u52a0\u8f7d\u4e0d\u540c\u7684 reducer \u3002 <br \/>\u6211\u89c9\u5f97\u5e94\u8be5\u53ef\u4ee5\u7528\u5143\u7f16\u7a0b\u53bb\u641e\u5b9a\uff0c\u628a\u4f60\u7684\u5177\u4f53\u7684\u51fd\u6570\u7528 `\u6ce8\u89e3` \u53bb Reflect.defineMetadata\uff0c\u7136\u540e\u7528\u7684\u65f6\u5019\uff0c\u6839\u636e action \u7684 type \u7528 Reflect.getMetadata \u53bb\u904d\u5386\uff0c\u627e\u5230\u5bf9\u5e94\u7684\u51fd\u6570\uff0c\u6267\u884c\u5b83\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1331992\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : KuroNekoFan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u6709\u70b9\u597d\u5947\u4e3a\u4ec0\u4e48\u4e00\u4e2a ui \u72b6\u6001\u4f1a\u7528 redux \u6765\u63a7\u5236                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1331993\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u662f\u89c9\u5f97\uff0cswitch case \u592a\u591a\u4e86\u4e48\uff1f\u505a\u4e2a map \u6216\u8005 table \u6620\u5c04\u4e00\u4e0b\u3002\u901f\u5ea6\u5feb\uff0c\u5199\u8d77\u6765\u4e5f\u4e0d\u9ebb\u70e6\u3002<br \/>p.s \u4ee3\u7801\u592a\u957f\u4e86\u2026\u2026 \u624b\u673a\u770b\u4e0d\u6e05\u695a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1331994\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : MintZX <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6bcf\u4e2a\u4e0d\u540c\u79cd\u7c7b\u7684 reducer \u62ff\u51fa\u6765\u5355\u72ec\u505a\u4e00\u4e2a\u6587\u4ef6\u7136\u540e\u7528 combine<\/p>\n<p>\u547d\u540d\u9075\u5faa\u4e00\u79cd\u65b9\u5f0f\uff0ccamelcase \u6216\u8005 underscore \u3002\u4e0d\u5efa\u8bae\u6df7\u7528\u3002<\/p>\n<p>\u8bd5\u8bd5 redux toolkit                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1331995\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wd <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u4ed4\u7ec6\u770b\uff0c\u4f46\u662f\u7b2c\u4e00\u6bb5\u4ee3\u7801\u597d\u50cf\u662f\u5199\u4e86\u4e00\u5927\u5806\u4ee3\u7801\u53ea\u662f\u4e3a\u4e86\u540d\u5b57\u77ed\u4e00\u70b9\uff1f\u54ea\u4e3a\u5565\u4e0d\u76f4\u63a5\u53d6\u4e00\u4e2a\u77ed\u540d\u5b57&#8230;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1331996\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zhw2590582 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u597d\u4e45\u6ca1\u770b\u8fc7\u8fd9\u79cd\u957f\u547d\u540d\u7684\u53d8\u91cf\uff0c\u8bfb\u8d77\u6765\u6709\u70b9\u614c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1331997\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : stone520 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u547d\u540d\u4e5f\u592a\u957f\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1331998\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : baxtergu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             reducer \u62c6\u5206\u4e0b \u7136\u540e\u7528 combineReducer \u7684\u65b9\u5f0f\u4f1a\u6e05\u695a\u4e00\u70b9\u3002\u5982\u679c\u8fd8\u4e0d\u884c\u7684\u8bdd\u5c1d\u8bd5\u8003\u8651\u7528 middleware \u53bb\u505a\u66f4\u9ad8\u7ea7\u7684\u5904\u7406                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1331999\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kimiler <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @KuroNekoFan \u662f\u7684\u5440\uff0c\u4e0d\u7136\u548b\u63a7\u5236\u5462\uff0c\u6211\u6ca1\u60f3\u5230\u597d\u7684\u529e\u6cd5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1332000\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kimiler <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @MintZX \u597d\u7684\uff0c\u6211\u4e86\u89e3\u4e86\u89e3\uff0c\u8c22\u8c22\u8001\u54e5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1332001\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : kimiler <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @baxtergu \u73b0\u5728\u5df2\u7ecf\u7528\u4e86 combine \u4e86\uff0c\u4e0d\u8fc7\u4e0a\u5c42\u662f\u6839\u636e\u6a21\u677f\u53bb\u5212\u5206\uff0c\u73b0\u5728\u6211\u4eec\u4e00\u4e2a\u9875\u9762\u9700\u8981\u8fde\u7eed\u8c03\u7528\u5341\u51e0\u4e2a\u63a5\u53e3\uff0c\u6240\u4ee5\u770b\u8d77\u6765\u53c8\u597d\u591a state                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1332002\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yuanfnadi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             typescript-fsa<\/p>\n<p>dva-model-creator                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1332003\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yuanfnadi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/github.com\/webclipper\/web-clipper\/blob\/2931a5433f\/src\/store\/reducers\/clipper.ts<\/p>\n<p>\u53ef\u4ee5\u770b\u4e00\u4e0b\u8fd9\u4e2a\u9879\u76ee\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42\u6307\u6559\u4e00\u4e2a redux \u76f8\u5173\u7684\u95ee\u9898&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/89989"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=89989"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/89989\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=89989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=89989"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=89989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}