{"id":5897,"date":"2023-01-06T15:30:26","date_gmt":"2023-01-06T07:30:26","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5897"},"modified":"2023-01-06T15:30:26","modified_gmt":"2023-01-06T07:30:26","slug":"react%e4%b8%ad%e4%bd%bf%e7%94%a8redux%e6%99%82%e5%a6%82%e4%bd%95%e8%a8%ad%e8%a8%88action%e5%92%8creducer%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/react%e4%b8%ad%e4%bd%bf%e7%94%a8redux%e6%99%82%e5%a6%82%e4%bd%95%e8%a8%ad%e8%a8%88action%e5%92%8creducer%ef%bc%9f\/","title":{"rendered":"React\u4e2d\u4f7f\u7528Redux\u6642\u5982\u4f55\u8a2d\u8a08Action\u548cReducer\uff1f"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, Redux, Action, Reducer\"><\/p>\n<h1>React \u4e2d\u4f7f\u7528 Redux \u6642\u5982\u4f55\u8bbe\u8ba1 Action \u548c Reducer\uff1f<\/h1>\n<p>React \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\u7684 JavaScript \u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\u3002\u800c Redux \u662f\u4e00\u500b\u7528\u65bc\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u72c0\u614b\u7684 JavaScript \u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\u3002\u5728 React \u4e2d\u4f7f\u7528 Redux \u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u8a2d\u8a08 Action \u548c Reducer \u4f86\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u8a2d\u8a08 Action \u548c Reducer \u4f86\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\u3002<\/p>\n<h2>Action<\/h2>\n<p>Action \u662f\u4e00\u500b JavaScript \u5c0d\u8c61\uff0c\u5b83\u4ee3\u8868\u4e86\u4e00\u500b\u61c9\u7528\u7a0b\u5e8f\u7684\u52d5\u4f5c\uff0c\u5b83\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u8cc7\u6599\uff0c\u4f46\u5fc5\u9808\u5305\u542b\u4e00\u500b type \u5c6c\u6027\uff0c\u7528\u65bc\u6307\u5b9a\u52d5\u4f5c\u7684\u985e\u578b\u3002Action \u5c0d\u8c61\u53ef\u4ee5\u4f7f\u7528 dispatch() \u65b9\u6cd5\u50b3\u905e\u7d66 store\uff0cstore \u5c07\u6703\u628a\u5b83\u5011\u50b3\u905e\u7d66 reducer\u3002<\/p>\n<pre class=\"brush: javascript\">\n\/\/ Action \u5c0d\u8c61\nconst action = {\n  type: 'ADD_TODO',\n  payload: {\n    text: 'Learn Redux'\n  }\n};\n\n\/\/ \u50b3\u905e Action \u5c0d\u8c61\u7d66 store\ndispatch(action);\n<\/pre>\n<h2>Reducer<\/h2>\n<p>Reducer \u662f\u4e00\u500b\u51fd\u6578\uff0c\u5b83\u63a5\u6536\u5169\u500b\u53c3\u6578\uff1a\u7576\u524d\u7684\u72c0\u614b\u548c\u50b3\u905e\u7d66 store \u7684 action\uff0c\u5b83\u6703\u6839\u64da action \u7684 type \u5c6c\u6027\u4f86\u66f4\u65b0\u72c0\u614b\uff0c\u4e26\u8fd4\u56de\u65b0\u7684\u72c0\u614b\u3002<\/p>\n<pre class=\"brush: javascript\">\n\/\/ Reducer \u51fd\u6578\nconst reducer = (state, action) => {\n  switch (action.type) {\n    case 'ADD_TODO':\n      return {\n        ...state,\n        todos: [...state.todos, action.payload]\n      };\n    default:\n      return state;\n  }\n};\n<\/pre>\n<p>\u5728 React \u4e2d\u4f7f\u7528 Redux \u6642\uff0c\u958b\u767c\u8005\u9700\u8981\u8a2d\u8a08 Action \u548c Reducer \u4f86\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\u3002Action \u662f\u4e00\u500b JavaScript \u5c0d\u8c61\uff0c\u5b83\u4ee3\u8868\u4e86\u4e00\u500b\u61c9\u7528\u7a0b\u5e8f\u7684\u52d5\u4f5c\uff0c\u5b83\u53ef\u4ee5\u5305\u542b\u4efb\u4f55\u8cc7\u6599\uff0c\u4f46\u5fc5\u9808\u5305\u542b\u4e00\u500b type \u5c6c\u6027\uff0c\u7528\u65bc\u6307\u5b9a\u52d5\u4f5c\u7684\u985e\u578b\u3002Reducer \u662f\u4e00\u500b\u51fd\u6578\uff0c\u5b83\u63a5\u6536\u5169\u500b\u53c3\u6578\uff1a\u7576\u524d\u7684\u72c0\u614b\u548c\u50b3\u905e\u7d66 store \u7684 action\uff0c\u5b83\u6703\u6839\u64da action \u7684 type \u5c6c\u6027\u4f86\u66f4\u65b0\u72c0\u614b\uff0c\u4e26\u8fd4\u56de\u65b0\u7684\u72c0\u614b\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4f7f\u7528 Action \u548c Reducer \u4f86\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\uff0c\u4e26\u4e14\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u64b0\u5beb\u53ef\u91cd\u7528\u7684\u7a0b\u5f0f\u78bc\u3002\u56e0\u6b64\uff0c\u5728 React \u4e2d\u4f7f\u7528 Redux \u6642\uff0c\u958b\u767c\u8005\u61c9\u8a72\u8a2d\u8a08 Action \u548c Reducer \u4f86\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39React\u4e2d\u4f7f\u7528Redux\u6642\u5982\u4f55\u8a2d\u8a08Action\u548cReducer\uff0c\u5305\u62ecAction\u7684\u5b9a\u7fa9\u3001Reducer\u7684\u5b9a\u7fa9\u3001Action\u548cReducer\u4e4b\u9593\u7684\u95dc\u4fc2\u7b49\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-5897","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1x7","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5897"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5897\/revisions"}],"predecessor-version":[{"id":5898,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5897\/revisions\/5898"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}