{"id":5713,"date":"2025-06-01T13:36:01","date_gmt":"2025-06-04T06:17:01","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5713"},"modified":"2025-06-04T13:36:01","modified_gmt":"2025-06-04T06:17:01","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8redux%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8redux%ef%bc%9f\/","title":{"rendered":"\u638c\u63e1 Redux\uff1a\u5728 React \u61c9\u7528\u7a0b\u5f0f\u4e2d\u7ba1\u7406\u72c0\u614b\u7684\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, Redux, React.js, \u524d\u7aef\u958b\u767c, \u72c0\u614b\u7ba1\u7406, JavaScript\"><\/p>\n<h1>\u638c\u63e1 Redux\uff1a\u5728 React \u61c9\u7528\u7a0b\u5f0f\u4e2d\u7ba1\u7406\u72c0\u614b\u7684\u5b8c\u6574\u6307\u5357<\/h1>\n<p>\u5728\u73fe\u4ee3\u7684\u524d\u7aef\u958b\u767c\u4e2d\uff0c\u72c0\u614b\u7ba1\u7406\u662f\u4e00\u500b\u95dc\u9375\u7684\u6311\u6230\uff0c\u7279\u5225\u662f\u7576\u61c9\u7528\u7a0b\u5f0f\u898f\u6a21\u589e\u5927\u6642\u3002Redux \u4f5c\u70ba\u4e00\u500b\u6d41\u884c\u7684 JavaScript \u51fd\u5f0f\u5eab\uff0c\u70ba React \u61c9\u7528\u7a0b\u5f0f\u63d0\u4f9b\u4e86\u4e00\u7a2e\u9ad8\u6548\u4e14\u53ef\u9810\u6e2c\u7684\u72c0\u614b\u7ba1\u7406\u89e3\u6c7a\u65b9\u6848\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0e\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528 Redux\uff0c\u4e26\u5e6b\u52a9\u60a8\u8f15\u9b06\u5730\u7ba1\u7406\u61c9\u7528\u7a0b\u5f0f\u7684\u72c0\u614b\u3002<\/p>\n<h2>Redux \u662f\u4ec0\u9ebc\uff1f<\/h2>\n<p>Redux \u662f\u4e00\u500b\u8f15\u91cf\u7d1a\u7684\u72c0\u614b\u7ba1\u7406\u5eab\uff0c\u57fa\u65bc Flux \u67b6\u69cb\uff0c\u65e8\u5728\u70ba JavaScript \u61c9\u7528\u7a0b\u5f0f\u63d0\u4f9b\u4e00\u500b\u53ef\u9810\u6e2c\u7684\u72c0\u614b\u7ba1\u7406\u65b9\u5f0f\u3002\u5176\u6838\u5fc3\u7406\u5ff5\u662f\u300c\u55ae\u4e00\u6e90\u771f\u7406\u300d\uff0c\u5373\u6240\u6709\u61c9\u7528\u7a0b\u5f0f\u7684\u72c0\u614b\u90fd\u61c9\u8a72\u5132\u5b58\u5728\u4e00\u500b\u55ae\u4e00\u7684\u4f4d\u7f6e\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u6578\u64da\u4e0d\u4e00\u81f4\u7684\u554f\u984c\u4e26\u7c21\u5316\u72c0\u614b\u8b8a\u66f4\u7684\u8ffd\u8e64\u3002<\/p>\n<h2>\u5728 React \u4e2d\u5982\u4f55\u4f7f\u7528 Redux\uff1f<\/h2>\n<p>\u8981\u5728 React \u4e2d\u4f7f\u7528 Redux\uff0c\u9996\u5148\u60a8\u9700\u8981\u5b89\u88dd `redux` \u548c `react-redux` \u9019\u5169\u500b\u5957\u4ef6\u3002\u60a8\u53ef\u4ee5\u901a\u904e npm \u6216 yarn \u4f86\u5b89\u88dd\u5b83\u5011\uff1a<\/p>\n<pre class=\"brush: python\">\nnpm install redux react-redux\n<\/pre>\n<p>\u5b89\u88dd\u5b8c\u6210\u5f8c\uff0c\u4e0b\u4e00\u6b65\u662f\u5275\u5efa\u4e00\u500b Redux store\uff0c\u9019\u5c07\u662f\u60a8\u61c9\u7528\u7a0b\u5f0f\u7684\u72c0\u614b\u7684\u55ae\u4e00\u4f86\u6e90\u3002\u4f7f\u7528 `createStore` \u51fd\u5f0f\u4f86\u5275\u5efa store\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre class=\"brush: python\">\nimport { createStore } from 'redux';\n\n\/\/ \u5047\u8a2d\u60a8\u5df2\u7d93\u5b9a\u7fa9\u4e86\u4e00\u500b reducer\nconst store = createStore(reducer);\n<\/pre>\n<p>\u63a5\u8457\uff0c\u4f7f\u7528 `Provider` \u7d44\u4ef6\u5c07 store \u6ce8\u5165\u5230 React \u61c9\u7528\u7a0b\u5f0f\u4e2d\uff0c\u9019\u6a23\u6240\u6709\u5b50\u7d44\u4ef6\u90fd\u53ef\u4ee5\u8a2a\u554f\u5230 Redux store\uff1a<\/p>\n<pre class=\"brush: python\">\nimport { Provider } from 'react-redux';\nimport ReactDOM from 'react-dom';\n\nReactDOM.render(\n  <Provider store={store}>\n    <App \/>\n  <\/Provider>,\n  document.getElementById('root')\n);\n<\/pre>\n<p>\u73fe\u5728\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 `connect` \u51fd\u5f0f\u5c07 React \u7d44\u4ef6\u8207 Redux store \u9023\u63a5\u8d77\u4f86\uff0c\u65b9\u4fbf\u5730\u5f9e store \u4e2d\u7372\u53d6\u72c0\u614b\u4e26\u6d3e\u767c\u884c\u70ba\uff08Actions\uff09\uff1a<\/p>\n<pre class=\"brush: python\">\nimport { connect } from 'react-redux';\n\nconst mapStateToProps = (state) => {\n  return {\n    count: state.count \/\/ \u5f9e Redux store \u7372\u53d6 count \u5c6c\u6027\n  };\n};\n\nconst mapDispatchToProps = (dispatch) => {\n  return {\n    increment: () => dispatch({ type: 'INCREMENT' }) \/\/ \u6d3e\u767c\u589e\u52a0\u8a08\u6578\u7684\u884c\u70ba\n  };\n};\n\nconst Counter = connect(mapStateToProps, mapDispatchToProps)(CounterComponent);\n<\/pre>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u60a8\u5c31\u53ef\u4ee5\u5728 React \u61c9\u7528\u7a0b\u5f0f\u4e2d\u8f15\u9b06\u5730\u4f7f\u7528 Redux \u4f86\u7ba1\u7406\u72c0\u614b\u4e86\uff01<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u672c\u6587\u8a73\u7d30\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528 Redux \u9032\u884c\u72c0\u614b\u7ba1\u7406\u3002\u6211\u5011\u5f9e Redux \u7684\u6838\u5fc3\u6982\u5ff5\u958b\u59cb\uff0c\u7136\u5f8c\u9010\u6b65\u5f15\u5c0e\u60a8\u5b8c\u6210\u5728 React \u61c9\u7528\u7a0b\u5f0f\u4e2d\u5b89\u88dd\u548c\u914d\u7f6e Redux \u7684\u904e\u7a0b\u3002\u7121\u8ad6\u60a8\u662f\u521d\u5b78\u8005\u9084\u662f\u6709\u7d93\u9a57\u7684\u958b\u767c\u8005\uff0c\u638c\u63e1 Redux \u5c07\u6709\u52a9\u65bc\u60a8\u66f4\u9ad8\u6548\u5730\u69cb\u5efa\u53ef\u64f4\u5c55\u7684\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n<p>\u5e0c\u671b\u9019\u7bc7\u6587\u7ae0\u80fd\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u7406\u89e3 Redux \u7684\u4f7f\u7528\uff0c\u4e26\u5728\u672a\u4f86\u7684\u5c08\u6848\u4e2d\u5145\u5206\u767c\u63ee\u5b83\u7684\u6f5b\u529b\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Redux\uff0c\u5f9e\u5b89\u88ddRedux\u5230\u5efa\u7acbReduxStore\uff0c\u4ee5\u53ca\u5982\u4f55\u5c07Redux\u8207React\u7d50\u5408\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u4f7f\u7528Redux\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-5713","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-1u9","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5713","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=5713"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5713\/revisions"}],"predecessor-version":[{"id":5714,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5713\/revisions\/5714"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5713"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5713"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5713"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}