{"id":88872,"date":"2024-04-07T17:39:50","date_gmt":"2024-04-07T09:39:50","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/88872.html"},"modified":"2024-04-07T17:39:52","modified_gmt":"2024-04-07T09:39:52","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8reducer%e8%bf%9b%e8%a1%8c%e7%8a%b6%e6%80%81%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/88872.html","title":{"rendered":"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Reducer\u8fdb\u884c\u72b6\u6001\u7ba1\u7406"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02172810\/eac5a05d-0251-40d0-8380-4ca561e8ddc8.webp\" alt=\"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Reducer\u8fdb\u884c\u72b6\u6001\u7ba1\u7406\" \/><\/p>\n<p><p>\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Reducer\u8fdb\u884c\u72b6\u6001\u7ba1\u7406\uff1f<\/p>\n<\/p>\n<p><p>\u5728React\u4e2d\uff0c\u4f7f\u7528Reducer\u8fdb\u884c\u72b6\u6001\u7ba1\u7406\u662f\u4e00\u79cd\u9ad8\u6548\u5e76\u4e14\u53ef\u9884\u6d4b\u72b6\u6001\u66f4\u65b0\u7684\u65b9\u6cd5\u3002<strong>Reducer\u662f\u4e00\u79cd\u7eaf\u51fd\u6570<\/strong>\uff0c\u5b83\u63a5\u53d7\u5f53\u524d\u72b6\u6001\u548c\u52a8\u4f5c\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u65b0\u7684\u72b6\u6001\uff0c\u5e76\u5141\u8bb8\u5e94\u7528\u5f00\u53d1\u8005\u6309\u7167\u4e00\u79cd\u4e32\u884c\u3001\u53ef\u9884\u6d4b\u7684\u65b9\u5f0f\u6765\u7ba1\u7406\u590d\u6742\u7684\u72b6\u6001\u903b\u8f91\u3002<strong>\u4e3b\u8981\u4f18\u70b9\u5305\u62ec\u7ef4\u62a4\u5e94\u7528\u903b\u8f91\u7684\u4e00\u81f4\u6027\u3001\u4fbf\u4e8e\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/strong>\uff0c\u4ee5\u53ca\u642d\u914d\u4f7f\u7528Context\u53ef\u907f\u514d\u7ec4\u4ef6\u5c42\u7ea7\u95f4\u7684\u201c\u9053\u5177\u6f02\u79fb\u201d\uff08prop drilling\uff09\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u5730\u8bf4\uff0cReducer\u51fd\u6570\u9075\u5faa\u4e0d\u53ef\u53d8\u6027\u539f\u5219\uff0c\u6bcf\u6b21\u90fd\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u72b6\u6001\u5bf9\u8c61\u800c\u4e0d\u662f\u4fee\u6539\u5f53\u524d\u72b6\u6001\u3002\u8fd9\u79cd\u65b9\u6cd5\u63d0\u5347\u4e86\u7a0b\u5e8f\u7684\u53ef\u9760\u6027\uff0c\u56e0\u4e3a\u5b83\u907f\u514d\u4e86\u76f4\u63a5\u4fee\u6539\u72b6\u6001\u6240\u5e26\u6765\u7684\u526f\u4f5c\u7528\uff0c\u800c\u4e14\u8fd9\u79cd\u6a21\u5f0f\u4e5f\u4fbf\u4e8e\u5b9e\u73b0\u64a4\u9500\/\u91cd\u505a\u7b49\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001REDUCER \u4e0e REACT USESTATE \u7684\u5bf9\u6bd4<\/p>\n<\/p>\n<p><p><strong>Reducer<\/strong> \u548c <strong>useState<\/strong> \u662fReact\u7ec4\u4ef6\u4e2d\u7ba1\u7406\u72b6\u6001\u7684\u4e24\u79cd\u4e0d\u540c\u65b9\u6cd5\u3002\u5728\u7b80\u5355\u7684\u573a\u666f\u4e0b\uff0cuseState\u8db3\u4ee5\u5904\u7406\u7ec4\u4ef6\u72b6\u6001\uff0c\u4f46\u5f53\u72b6\u6001\u903b\u8f91\u53d8\u5f97\u590d\u6742\u65f6\uff0c\u4f8b\u5982\u591a\u4e2a\u503c\u76f8\u4e92\u4f9d\u8d56\u6216\u4e0b\u4e00\u4e2a\u72b6\u6001\u4f9d\u8d56\u4e8e\u524d\u4e00\u4e2a\uff0cReducer\u5219\u663e\u5f97\u5c24\u5176\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>useState\u662f\u4e00\u4e2a\u6784\u5efa\u5728reducer\u4e4b\u4e0a\u7684\u94a9\u5b50<\/strong>\uff0c\u7528\u4e8e\u58f0\u660e\u5f0f\u5730\u7ba1\u7406\u7ec4\u4ef6\u72b6\u6001\u503c\u3002\u800c<strong>useReducer<\/strong> \u5219\u63d0\u4f9b\u4e86\u66f4\u4e3a\u8be6\u5c3d\u7684\u63a7\u5236\u65b9\u5f0f\uff0c\u5b83\u63a5\u6536\u4e00\u4e2areducer\u51fd\u6570\u6765\u5904\u7406\u590d\u6742\u7684\u72b6\u6001\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001REDUCER \u7684\u57fa\u672c\u6982\u5ff5<\/p>\n<\/p>\n<p><p>Reducer\u51fd\u6570\u7684\u57fa\u672c\u7279\u5f81\u5728\u4e8e\u5b83\u662f\u4e00\u4e2a\u7eaf\u51fd\u6570\uff0c\u8fd9\u610f\u5473\u7740\u5bf9\u4e8e\u540c\u6837\u7684\u8f93\u5165\u6c38\u8fdc\u4f1a\u8fd4\u56de\u76f8\u540c\u7684\u8f93\u51fa\uff0c\u4e14\u4e0d\u4f1a\u4ea7\u751f\u526f\u4f5c\u7528\u3002<strong>Reducer\u7684\u8fd4\u56de\u503c\u5fc5\u987b\u662f\u5168\u65b0\u7684\u5bf9\u8c61<\/strong>\uff0c\u4fdd\u8bc1\u4e86\u72b6\u6001\u7684\u4e0d\u53ef\u53d8\u6027\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u822c\u60c5\u51b5\u4e0b\uff0cReducer\u51fd\u6570\u6839\u636e\u4f20\u5165\u7684\u5f53\u524d\u72b6\u6001\uff08state\uff09\u548c\u52a8\u4f5c\uff08action\uff09\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u72b6\u6001\u3002\u52a8\u4f5c\u901a\u5e38\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5305\u542b\u63cf\u8ff0\u53d1\u751f\u4e86\u4ec0\u4e48\u7684<code>type<\/code>\u5b57\u6bb5\uff0c\u4ee5\u53ca\u9700\u8981\u66f4\u65b0\u72b6\u6001\u65f6\u4f7f\u7528\u7684\u5176\u5b83\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528USEREDUCER \u94a9\u5b50<\/p>\n<\/p>\n<p><p><code>useReducer<\/code>\u94a9\u5b50\u662fReact\u63d0\u4f9b\u7684\u4e00\u4e2a\u7528\u4e8e\u72b6\u6001\u7ba1\u7406\u7684\u94a9\u5b50\uff0c\u5176\u4f7f\u7528\u65b9\u5f0f\u4e0e<code>useState<\/code>\u94a9\u5b50\u76f8\u4f3c\uff0c\u4f46\u63d0\u4f9b\u4e86\u66f4\u4e3a\u5f3a\u5927\u7684\u529f\u80fd\u3002\u4f7f\u7528<code>useReducer<\/code>\u53ef\u4ee5\u66f4\u4e3a\u6e05\u6670\u5730\u5904\u7406\u5305\u542b\u591a\u4e2a\u5b50\u503c\u7684\u72b6\u6001\u5bf9\u8c61\uff0c\u6216\u5f53\u4e0b\u4e00\u4e2a\u72b6\u6001\u4f9d\u8d56\u4e8e\u524d\u4e00\u4e2a\u72b6\u6001\u65f6\u3002<strong><code>useReducer<\/code>\u8ba9\u4f60\u6709\u80fd\u529b\u901a\u8fc7\u6d3e\u53d1\u52a8\u4f5c\u6765\u63cf\u8ff0\u53d1\u751f\u4e86\u4ec0\u4e48<\/strong>\uff0c\u800creducer\u5219\u4f1a\u51b3\u5b9a\u5982\u4f55\u6839\u636e\u90a3\u4e9b\u52a8\u4f5c\u6765\u66f4\u65b0\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5b9e\u73b0useReducer\u94a9\u5b50\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2areducer\u51fd\u6570\u548c\u4e00\u4e2a\u521d\u59cb\u72b6\u6001\uff0c\u5e76\u4f5c\u4e3a\u53c2\u6570\u4f20\u7ed9useReducer\uff1b\u53e6\u5916\uff0c\u8fd8\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u53ef\u9009\u7684\u521d\u59cb\u52a8\u4f5c\u6765\u8bbe\u7f6e\u521d\u59cb\u72b6\u6001\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001IMPLEMENTING ACTIONS AND REDUCERS<\/p>\n<\/p>\n<p><p>\u5728\u72b6\u6001\u66f4\u65b0\u7684\u8fc7\u7a0b\u4e2d\uff0c<strong>\u52a8\u4f5c\u548creducers\u662f\u6838\u5fc3\u7684\u90e8\u5206<\/strong>\u3002\u52a8\u4f5c\u63cf\u8ff0\u4e86\u5e94\u8be5\u53d1\u751f\u7684\u72b6\u6001\u53d8\u5316\uff0c\u800creducers\u5219\u662f\u51b3\u5b9a\u5982\u4f55\u54cd\u5e94\u8fd9\u4e9b\u52a8\u4f5c\u5e76\u8fd4\u56de\u65b0\u72b6\u6001\u7684\u7eaf\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u521b\u5efa\u52a8\u4f5c\u65f6\uff0c\u5b83\u4eec\u901a\u5e38\u662f\u5177\u6709type\u5c5e\u6027\u7684\u5bf9\u8c61\uff0c\u53ef\u80fd\u8fd8\u5305\u542b\u989d\u5916\u7684\u6570\u636e\u3002<strong>Reducer\u51fd\u6570\u76f8\u5e94\u5730\u6839\u636e\u52a8\u4f5c\u7684type\u6765\u66f4\u65b0\u72b6\u6001<\/strong>\uff0c\u540c\u65f6\u6839\u636e\u9700\u8981\u8fdb\u884c\u6761\u4ef6\u5224\u65ad\u548c\u903b\u8f91\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u7ec4\u5408REDUCERS<\/p>\n<\/p>\n<p><p>\u5f53\u5e94\u7528\u53d8\u5f97\u590d\u6742\uff0c\u5e76\u4e14\u72b6\u6001\u903b\u8f91\u9700\u8981\u5728\u591a\u4e2areducers\u4e4b\u95f4\u5171\u4eab\u65f6\uff0c\u5c06reducers\u7ec4\u5408\u5728\u4e00\u8d77\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u6a21\u5f0f\u3002\u53ef\u4ee5\u4e3a\u5e94\u7528\u7684\u4e0d\u540c\u90e8\u5206\u521b\u5efa\u591a\u4e2a\u72ec\u7acb\u7684reducers\uff0c\u7136\u540e\u4f7f\u7528<code>combineReducers<\/code>\u51fd\u6570\u5c06\u5b83\u4eec\u5408\u5e76\u4e3a\u4e00\u4e2a\u5355\u4e00\u7684\u6839reducer\u3002<\/p>\n<\/p>\n<p><p><strong>\u7ec4\u5408reducers\u6709\u52a9\u4e8e\u4ee3\u7801\u7ef4\u62a4\u548c\u903b\u8f91\u5206\u79bb<\/strong>\uff0c\u6bcf\u4e2areducer\u90fd\u53ea\u5173\u5fc3\u5b83\u6240\u7ba1\u7406\u7684\u90a3\u90e8\u5206\u72b6\u6001\u3002\u8fd9\u6837\u505a\u8fd8\u80fd\u63d0\u5347\u6d4b\u8bd5\u7684\u4fbf\u5229\u6027\uff0c\u56e0\u4e3a\u4f60\u53ef\u4ee5\u72ec\u7acb\u5730\u6d4b\u8bd5\u6bcf\u4e2areducer\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5728REDUCER\u4e2d\u5904\u7406\u526f\u4f5c\u7528<\/p>\n<\/p>\n<p><p>\u901a\u5e38\u60c5\u51b5\u4e0b\uff0creducers\u5e94\u8be5\u4fdd\u6301\u7eaf\u51c0\u800c\u6ca1\u6709\u526f\u4f5c\u7528\uff0c\u5b83\u4eec\u53ea\u5e94\u8be5\u8ba1\u7b97\u65b0\u7684\u72b6\u6001\u3002\u4f46\u5728\u73b0\u5b9e\u7684\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5728\u89e6\u53d1\u67d0\u4e9b\u52a8\u4f5c\u65f6\u6267\u884c\u526f\u4f5c\u7528\uff0c\u4f8b\u5982API\u8c03\u7528\u7b49\u5f02\u6b65\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><p>\u9488\u5bf9\u8fd9\u4e00\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e2d\u95f4\u4ef6\u5982<code>redux-thunk<\/code>\u6216<code>redux-saga<\/code>\u6765\u5904\u7406\u5f02\u6b65\u903b\u8f91\u3002\u8fd9\u4e9b\u4e2d\u95f4\u4ef6\u5141\u8bb8\u4f60\u5728\u6d3e\u53d1\u52a8\u4f5c\u65f6\u6267\u884c\u526f\u4f5c\u7528\uff0c\u540c\u65f6\u4fdd\u6301reducer\u7eaf\u51c0\u548c\u72ec\u7acb\u4e8e\u526f\u4f5c\u7528\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528Context API\u4e0eREDUCER\u7ed3\u5408<\/p>\n<\/p>\n<p><p>\u5728React\u4e2d\uff0cContext API\u5141\u8bb8\u4f60\u5728\u7ec4\u4ef6\u95f4\u5171\u4eab\u72b6\u6001\u800c\u4e0d\u5fc5\u663e\u5f0f\u4f20\u9012prop\u3002\u5f53\u4f60\u5c06useReducer\u4e0eContext\u7ed3\u5408\u4f7f\u7528\u65f6\uff0c\u53ef\u4ee5\u907f\u514d\u7ec4\u4ef6\u5c42\u7ea7\u6df1\u7684\u72b6\u6001\u4f20\u9012\u95ee\u9898\uff0c\u5373\u901a\u5e38\u6240\u8bf4\u7684prop drilling\u3002<\/p>\n<\/p>\n<p><p>\u8981\u5b9e\u73b0\u8fd9\u4e00\u70b9\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2acontext\u5e76\u4f7f\u7528React\u7684Provider\u7ec4\u4ef6\u6765\u5305\u88f9\u5e94\u7528\u7684\u6839\u7ec4\u4ef6\u3002\u7136\u540e\u5728\u5e94\u7528\u7684\u5176\u4ed6\u90e8\u5206\u901a\u8fc7useContext\u94a9\u5b50\u83b7\u53d6\u72b6\u6001\u548c\u6d3e\u53d1\u51fd\u6570\u3002\u8fd9\u4e3a\u5927\u578b\u5e94\u7528\u7684\u72b6\u6001\u7ba1\u7406\u63d0\u4f9b\u4e86\u975e\u5e38\u6709\u529b\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u6027\u80fd\u4f18\u5316<\/p>\n<\/p>\n<p><p>\u867d\u7136\u4f7f\u7528reducer\u8fdb\u884c\u72b6\u6001\u7ba1\u7406\u5e26\u6765\u4e86\u5f88\u591a\u597d\u5904\uff0c\u4f46\u6211\u4eec\u4e5f\u9700\u8981\u8003\u8651\u5230\u6027\u80fd\u4f18\u5316\u7684\u95ee\u9898\u3002\u6bcf\u6b21\u72b6\u6001\u66f4\u65b0\u65f6\uff0cReact\u90fd\u4f1a\u91cd\u65b0\u6e32\u67d3\u7ec4\u4ef6\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u6e32\u67d3\u6b21\u6570\u589e\u52a0\u3002<\/p>\n<\/p>\n<p><p><strong>React\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7b56\u7565\u6765\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u6e32\u67d3<\/strong>\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>React.memo<\/code>\u6765\u5bf9\u7ec4\u4ef6\u8fdb\u884c\u5305\u88f9\uff0c\u8fd9\u5c06\u4f7f\u5f97\u53ea\u6709\u5728prop\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u7ec4\u4ef6\u624d\u4f1a\u91cd\u65b0\u6e32\u67d3\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>useCallback<\/code>\u548c<code>useMemo<\/code>\u94a9\u5b50\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u51fd\u6570\u91cd\u65b0\u521b\u5efa\u548c\u8ba1\u7b97\u503c\u7684\u91cd\u65b0\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u4eab\u53d7\u4f7f\u7528Reducer\u5e26\u6765\u7684\u7ed3\u6784\u5316\u548c\u53ef\u9884\u6d4b\u6027\u597d\u5904\u7684\u540c\u65f6\uff0c\u4e5f\u4e0d\u727a\u7272\u5e94\u7528\u7684\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u4e5d\u3001\u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9047\u5230\u9700\u8981\u5728\u591a\u4e2a\u7ec4\u4ef6\u95f4\u5171\u4eab\u72b6\u6001\u7684\u60c5\u51b5\u3002\u6bd4\u5982\uff0c\u7528\u6237\u8ba4\u8bc1\u72b6\u6001\u53ef\u80fd\u9700\u8981\u5728\u5f88\u591a\u5730\u65b9\u8bbf\u95ee\u548c\u66f4\u65b0\u3002\u5728\u8fd9\u6837\u7684\u573a\u666f\u4e0b\uff0c\u4f7f\u7528reducer\u548cContext API\u53ef\u4ee5\u9ad8\u6548\u5730\u5b9e\u73b0\u5168\u5c40\u72b6\u6001\u7ba1\u7406\u3002<\/p>\n<\/p>\n<p><p>\u6784\u5efa\u4e00\u4e2a\u8ba4\u8bc1reducer\u6765\u7ba1\u7406\u7528\u6237\u767b\u5f55\u3001\u767b\u51fa\u548c\u4f1a\u8bdd\u8fc7\u671f\u7b49\u52a8\u4f5c\u3002\u7136\u540e\uff0c\u901a\u8fc7Context API\u5728\u5e94\u7528\u7684\u4e0d\u540c\u90e8\u5206\u5171\u4eab\u8fd9\u4e2a\u72b6\u6001\u548c\u6d3e\u53d1\u51fd\u6570\u3002\u8fd9\u6837\uff0c\u5728\u4efb\u4f55\u7ec4\u4ef6\u5185\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u901a\u8fc7\u76f8\u540c\u7684\u63a5\u53e3\u548c\u9884\u671f\u7684\u903b\u8f91\u6765\u66f4\u65b0\u72b6\u6001\uff0c\u5927\u5927\u7b80\u5316\u4e86\u72b6\u6001\u7ba1\u7406\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5341\u3001\u7ed3\u8bba\u4e0e\u6700\u4f73\u5b9e\u8df5<\/p>\n<\/p>\n<p><p>\u5728React\u4e2d\u4f7f\u7528Reducer\u8fdb\u884c\u72b6\u6001\u7ba1\u7406\u63d0\u4f9b\u4e86\u4e00\u79cd\u7a33\u5b9a\u4e14\u6613\u4e8e\u7ef4\u62a4\u7684\u65b9\u6cd5\u6765\u5904\u7406\u590d\u6742\u7684\u72b6\u6001\u903b\u8f91\u3002\u901a\u8fc7\u7eaf\u51fd\u6570\u7684\u65b9\u5f0f\u51cf\u5c11\u4e86\u76f4\u63a5\u64cd\u4f5c\u72b6\u6001\u7684\u4e0d\u786e\u5b9a\u6027\uff0c\u7b80\u5316\u4e86\u6d4b\u8bd5\u8fc7\u7a0b\uff0c\u5e76\u901a\u8fc7\u4e0eContext\u7684\u7ed3\u5408\u63d0\u9ad8\u4e86\u7ec4\u4ef6\u95f4\u7684\u72b6\u6001\u5171\u4eab\u6548\u7387\u3002<\/p>\n<\/p>\n<p><p>\u4f5c\u4e3a\u6700\u4f73\u5b9e\u8df5\uff0c\u6211\u4eec\u5e94\u5f53\u9075\u5faaimmutability\u539f\u5219\u3001\u5408\u7406\u5206\u914dreducer\u903b\u8f91\u3001\u907f\u514d\u5728reducers\u4e2d\u6267\u884c\u526f\u4f5c\u7528\u3001\u7ec4\u5408\u4f7f\u7528Context API\u548cuseReducer\u3001\u4ee5\u53ca\u65f6\u523b\u5173\u6ce8\u6027\u80fd\u4f18\u5316\u3002\u6b63\u786e\u5e94\u7528\u8fd9\u4e9b\u539f\u5219\u548c\u6a21\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u6211\u4eec\u5728\u6784\u5efa\u5927\u578bReact\u5e94\u7528\u65f6\u4fdd\u6301\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. React\u4e2d\u7684Reducer\u662f\u7528\u6765\u505a\u4ec0\u4e48\u7684\uff1f<\/strong><br \/>\nReducer\u662fReact\u7684\u4e00\u79cd\u72b6\u6001\u7ba1\u7406\u5de5\u5177\uff0c\u7528\u4e8e\u7ba1\u7406\u7ec4\u4ef6\u4e2d\u7684\u72b6\u6001\uff0c\u4f7f\u5f97\u72b6\u6001\u7684\u66f4\u65b0\u53d8\u5f97\u66f4\u52a0\u53ef\u63a7\u548c\u53ef\u9884\u6d4b\u3002\u901a\u8fc7\u5c06\u72b6\u6001\u7684\u66f4\u65b0\u903b\u8f91\u5c01\u88c5\u5728Reducer\u4e2d\uff0c\u7ec4\u4ef6\u53ef\u4ee5\u66f4\u52a0\u4e13\u6ce8\u4e8e\u663e\u793a\u6570\u636e\u548c\u54cd\u5e94\u7528\u6237\u7684\u4ea4\u4e92\u3002<\/p>\n<p><strong>2. \u5728React\u4e2d\u5982\u4f55\u4f7f\u7528Reducer\u8fdb\u884c\u72b6\u6001\u7ba1\u7406\uff1f<\/strong><br \/>\n\u9996\u5148\uff0c\u4f60\u9700\u8981\u4f7f\u7528React\u7684useReducer Hook\u6765\u521b\u5efa\u4e00\u4e2aReducer\u548c\u521d\u59cb\u72b6\u6001\u3002\u7136\u540e\uff0c\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528dispatch\u51fd\u6570\u6765\u89e6\u53d1\u72b6\u6001\u7684\u66f4\u65b0\u3002\u5f53\u8c03\u7528dispatch\u65f6\uff0cReducer\u4f1a\u6839\u636e\u4f20\u5165\u7684action\u7c7b\u578b\u6765\u5224\u65ad\u5e94\u8be5\u5982\u4f55\u66f4\u65b0\u72b6\u6001\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u72b6\u6001\u3002\u6700\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528useState\u6765\u83b7\u53d6\u548c\u4f7f\u7528\u66f4\u65b0\u540e\u7684\u72b6\u6001\u3002<\/p>\n<p><strong>3. \u548cRedux\u76f8\u6bd4\uff0cReact\u4e2d\u7684Reducer\u6709\u4ec0\u4e48\u4f18\u52bf\uff1f<\/strong><br \/>\n\u76f8\u6bd4\u4e8eRedux\uff0cReact\u4e2d\u7684Reducer\u5177\u6709\u66f4\u8f7b\u91cf\u7ea7\u548c\u7b80\u5355\u7684\u8bed\u6cd5\u3002\u4f60\u4e0d\u9700\u8981\u5f15\u5165\u989d\u5916\u7684\u5e93\u6216\u914d\u7f6e\uff0c\u53ea\u9700\u4f7f\u7528React\u5185\u7f6e\u7684useReducer Hook\u5373\u53ef\u3002\u6b64\u5916\uff0c\u4f7f\u7528Reducer\u8fdb\u884c\u72b6\u6001\u7ba1\u7406\u4e5f\u53ef\u4ee5\u66f4\u597d\u5730\u4e0eReact\u7684\u51fd\u6570\u5f0f\u7ec4\u4ef6\u548cHooks\u673a\u5236\u7ed3\u5408\uff0c\u4f7f\u5f97\u4ee3\u7801\u66f4\u52a0\u5177\u6709\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Reducer\u8fdb\u884c\u72b6\u6001\u7ba1\u7406\uff1f \u5728React\u4e2d\uff0c\u4f7f\u7528Reducer\u8fdb\u884c\u72b6\u6001\u7ba1\u7406\u662f\u4e00\u79cd\u9ad8\u6548\u5e76 [&hellip;]","protected":false},"author":3,"featured_media":88877,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/88872"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=88872"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/88872\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/88877"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=88872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=88872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=88872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}