{"id":5945,"date":"2023-01-06T15:43:21","date_gmt":"2023-01-06T07:43:21","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5945"},"modified":"2023-01-06T15:43:21","modified_gmt":"2023-01-06T07:43:21","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redux%e5%9c%a8react%e4%b8%ad%e9%80%b2%e8%a1%8c%e7%8b%80%e6%85%8b%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8redux%e5%9c%a8react%e4%b8%ad%e9%80%b2%e8%a1%8c%e7%8b%80%e6%85%8b%e7%ae%a1%e7%90%86\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Redux\u5728React\u4e2d\u9032\u884c\u72c0\u614b\u7ba1\u7406"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, Redux, \u72b6\u6001\u7ba1\u7406\"><\/p>\n<h1>\u5728 React \u4e2d\u5982\u4f55\u4f7f\u7528 Redux \u8fdb\u884c\u72b6\u6001\u7ba1\u7406\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\u7136\u800c\uff0cReact \u672c\u8eab\u4e26\u4e0d\u63d0\u4f9b\u72b6\u6001\u7ba1\u7406\u529f\u80fd\uff0c\u56e0\u6b64\u958b\u767c\u8005\u5fc5\u9808\u5c0b\u627e\u5176\u4ed6\u65b9\u6cd5\u4f86\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\u3002Redux \u662f\u4e00\u500b\u53d7\u6b61\u8fce\u7684\u72b6\u614b\u7ba1\u7406\u5eab\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728 React \u61c9\u7528\u7a0b\u5e8f\u4e2d\u66f4\u8f15\u9b06\u5730\u7ba1\u7406\u72c0\u614b\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528 Redux \u4f86\u7ba1\u7406\u72c0\u614b\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Redux\uff1f<\/h2>\n<p>Redux \u662f\u4e00\u500b JavaScript \u5eab\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728 React \u61c9\u7528\u7a0b\u5e8f\u4e2d\u66f4\u8f15\u9b06\u5730\u7ba1\u7406\u72c0\u614b\u3002\u5b83\u4f7f\u7528\u55ae\u4e00\u72c0\u614b\u6a39\u4f86\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u6240\u6709\u72c0\u614b\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u7a2e\u53ef\u9810\u6e2c\u7684\u65b9\u6cd5\u4f86\u66f4\u65b0\u72c0\u614b\u3002\u5b83\u9084\u63d0\u4f9b\u4e86\u4e00\u7a2e\u53ef\u64f4\u5c55\u7684\u67b6\u69cb\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u5927\u578b\u61c9\u7528\u7a0b\u5e8f\u3002<\/p>\n<h2>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528 Redux\uff1f<\/h2>\n<p>\u8981\u5728 React \u4e2d\u4f7f\u7528 Redux\uff0c\u958b\u767c\u8005\u9996\u5148\u9700\u8981\u5b89\u88dd Redux \u5eab\uff0c\u53ef\u4ee5\u4f7f\u7528 npm \u6216 yarn \u4f86\u5b89\u88dd\uff1a<\/p>\n<pre class=\"brush: javascript\">\nnpm install redux\n<\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u4f7f\u7528 Redux \u7684 API \u4f86\u5275\u5efa\u548c\u7ba1\u7406\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\u3002\u9996\u5148\uff0c\u958b\u767c\u8005\u9700\u8981\u5275\u5efa\u4e00\u500b\u5132\u5b58\u5668\uff0c\u5b83\u5c07\u7528\u65bc\u5132\u5b58\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst store = createStore(reducer);\n<\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u958b\u767c\u8005\u9700\u8981\u5275\u5efa\u4e00\u500b reducer\uff0c\u5b83\u5c07\u7528\u65bc\u66f4\u65b0\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\uff1a<\/p>\n<pre class=\"brush: javascript\">\nfunction reducer(state, action) {\n  switch (action.type) {\n    case 'UPDATE_STATE':\n      return {\n        ...state,\n        data: action.payload\n      };\n    default:\n      return state;\n  }\n}\n<\/pre>\n<p>\u6700\u5f8c\uff0c\u958b\u767c\u8005\u9700\u8981\u5c07\u5132\u5b58\u5668\u50b3\u905e\u7d66 React \u61c9\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre class=\"brush: javascript\">\nReactDOM.render(\n  <Provider store={store}>\n    <App \/>\n  <\/Provider>,\n  document.getElementById('root')\n);\n<\/pre>\n<p>\u9019\u6a23\uff0c\u958b\u767c\u8005\u5c31\u53ef\u4ee5\u5728 React \u61c9\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Redux \u4f86\u7ba1\u7406\u72c0\u614b\u4e86\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Redux \u662f\u4e00\u500b\u53d7\u6b61\u8fce\u7684\u72b6\u614b\u7ba1\u7406\u5eab\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728 React \u61c9\u7528\u7a0b\u5e8f\u4e2d\u66f4\u8f15\u9b06\u5730\u7ba1\u7406\u72c0\u614b\u3002\u8981\u5728 React \u4e2d\u4f7f\u7528 Redux\uff0c\u958b\u767c\u8005\u9700\u8981\u5b89\u88dd Redux \u5eab\uff0c\u7136\u5f8c\u5275\u5efa\u4e00\u500b\u5132\u5b58\u5668\u548c\u4e00\u500b reducer\uff0c\u6700\u5f8c\u5c07\u5132\u5b58\u5668\u50b3\u905e\u7d66 React \u61c9\u7528\u7a0b\u5e8f\u3002\u9019\u6a23\uff0c\u958b\u767c\u8005\u5c31\u53ef\u4ee5\u5728 React \u61c9\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528 Redux \u4f86\u7ba1\u7406\u72c0\u614b\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Redux\u5728React\u4e2d\u9032\u884c\u72c0\u614b\u7ba1\u7406\uff0c\u5f9eRedux\u7684\u57fa\u672c\u6982\u5ff5\u5230\u5982\u4f55\u5c07Redux\u8207React\u7d50\u5408\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528Redux\u4f86\u7ba1\u7406React\u61c9\u7528\u7a0b\u5e8f\u7684\u72c0\u614b\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-5945","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-1xT","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5945","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=5945"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5945\/revisions"}],"predecessor-version":[{"id":5946,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5945\/revisions\/5946"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}