{"id":134,"date":"2016-12-15T14:07:30","date_gmt":"2016-12-15T06:07:30","guid":{"rendered":"https:\/\/www.jarvisw.com\/?p=134"},"modified":"2016-12-15T15:29:19","modified_gmt":"2016-12-15T07:29:19","slug":"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3react-redux","status":"publish","type":"post","link":"https:\/\/www.jarvisw.com\/?p=134","title":{"rendered":"\u6df1\u5165\u7406\u89e3react-redux"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-135 size-full\" src=\"https:\/\/www.jarvisw.com\/wp-content\/uploads\/2016\/12\/bg2016092101.jpg\" width=\"750\" height=\"422\" srcset=\"https:\/\/www.jarvisw.com\/wp-content\/uploads\/2016\/12\/bg2016092101.jpg 750w, https:\/\/www.jarvisw.com\/wp-content\/uploads\/2016\/12\/bg2016092101-300x169.jpg 300w, https:\/\/www.jarvisw.com\/wp-content\/uploads\/2016\/12\/bg2016092101-180x100.jpg 180w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><\/p>\n<p>\u4e4b\u524d\u5728Jarvis OJ\u7684\u524d\u7aef\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u4e86react+redux\u7684\u6570\u636e\u6d41\u7ec4\u7ec7\u5f62\u5f0f\uff0c\u7531\u4e8eredux\u53ea\u662f\u4e00\u4e2a\u901a\u7528\u7684Javascript\u6a21\u5757\uff0c\u672c\u8eab\u548cReact\u5e76\u6ca1\u6709\u4ec0\u4e48\u5173\u7cfb\uff0c\u6240\u6709\u5c31\u6709\u4e86\u4e00\u4e2areact-redux\u7684\u9879\u76ee\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e9bapi\u7528\u4e8ereact\u548credux\u4e4b\u95f4\u66f4\u65b9\u4fbf\u7684\u8fde\u63a5\u548c\u66f4\u79d1\u5b66\u7684\u4ee3\u7801\u7ec4\u7ec7\u65b9\u5f0f\u3002\u8bb0\u5f97\u5f53\u65f6\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u53c2\u8003\u4e86\u7f51\u4e0a\u8bb8\u591a\u7684\u6559\u7a0b\u548c\u4f8b\u5b50\uff0c\u4f46\u8bb8\u591a\u6587\u7ae0\u53ea\u662f\u7ed9\u51fa\u4e86demo\u6216\u8005\u5199\u6cd5\uff0c\u5e76\u672a\u7ed9\u51fa\u4e3a\u4ec0\u4e48\u8981\u8fd9\u6837\u5199\u7684\u6df1\u5c42\u539f\u56e0\uff0c\u5f53\u65f6\u5f00\u53d1\u7684\u65f6\u5019\u6211\u4e5f\u53ea\u662f\u4f9d\u6837\u753b\u846b\u82a6\u5e76\u672a\u6df1\u7a76\u5176\u6240\u4ee5\u7136\uff0c\u76f4\u5230\u6700\u8fd1\uff0c\u6211\u53d1\u73b0\u7a81\u7136\u7406\u89e3\u4e86react+redux\u4e3a\u4ec0\u4e48\u8981\u5199\u6210\u8fd9\u6837\u7684\u6df1\u5c42\u539f\u56e0\uff0c\u56e0\u6b64\u672c\u6587\u672c\u7740\u63a2\u7a76\u7684\u76ee\u7684\uff0c\u8c08\u8c08\u6211\u5bf9react+redux\u9879\u76ee\u5199\u6cd5\u7684\u7406\u89e3\u3002<\/p>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u5148\u5410\u69fd\u5410\u69fd\uff0c\u6b63\u5982\u4e00\u4e9b\u7f51\u53cb\u6240\u8bf4\u7684\uff0creact\u7684\u4f5c\u8005\u4fe1\u5949unix\u54f2\u5b66\uff0c\u5e0c\u671b\u628areact\u6253\u9020\u6210\u4e00\u4e2a\u5c0f\u800c\u7f8e\u7684\u6846\u67b6\uff0c\u53ea\u63d0\u4f9b\u57fa\u672c\u7684\u529f\u80fd\uff0c\u800c\u4e0d\u50cfAngularJS\u90a3\u6837\u662f\u4e00\u4e2aall-in-one\u7684\u6574\u4f53\u89e3\u51b3\u65b9\u6848\u3002\u4e5f\u6b63\u662f\u56e0\u4e3a\u5982\u6b64\uff0c\u5982\u679c\u8981\u4f7f\u7528react\u5b8c\u6210\u4e00\u4e2a\u4ea4\u4e92\u590d\u6742\u7684\u5e9e\u5927\u5355\u9875\u9762\u5e94\u7528\u662f\u8fdc\u8fdc\u4e0d\u591f\u7684\uff0c\u8fd8\u9700\u8981\u501f\u52a9\u4e00\u4e9b\u5176\u4ed6\u7684\u7b2c\u4e09\u65b9\u6846\u67b6\uff0c\u672c\u6587\u6240\u63d0\u5230\u7684redux\u5c31\u662f\u5176\u4e2d\u4e4b\u4e00\u3002\u4f46\u662f\u7ecf\u8fc7\u4e00\u756a\u5f00\u53d1\u4f53\u9a8c\u4e5f\u4f1a\u53d1\u73b0\uff0c\u4e5f\u6b63\u5982\u5927\u5bb6\u6240\u8bf4\uff0c\u5728react\u52a0\u4e0a\u4e86\u8fd9\u4e9b\u7b2c\u4e09\u65b9\u6846\u67b6\u4e4b\u540e\uff0c\u6574\u4f53\u4ee3\u7801\u53cd\u800c\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\uff0c\u6bd4\u5982angularJS\u53ea\u9700\u8981100\u591a\u884c\u5c31\u53ef\u4ee5\u5b8c\u6210\u7684\u4e00\u4e2a\u6570\u636e\u6d41\u7ed1\u5b9a\u64cd\u4f5c\u7684demo\u9875\u9762\uff0c\u800c\u4f7f\u7528react+redux\u7684\u65b9\u5f0f\uff0c\u4e00\u4e2a\u57fa\u672cdemo\u5c31\u957f\u6210\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-137 \" src=\"https:\/\/www.jarvisw.com\/wp-content\/uploads\/2016\/12\/\u5c4f\u5e55\u5feb\u7167-2016-12-15-\u4e0b\u53481.23.03.png\" width=\"336\" height=\"347\" srcset=\"https:\/\/www.jarvisw.com\/wp-content\/uploads\/2016\/12\/\u5c4f\u5e55\u5feb\u7167-2016-12-15-\u4e0b\u53481.23.03.png 678w, https:\/\/www.jarvisw.com\/wp-content\/uploads\/2016\/12\/\u5c4f\u5e55\u5feb\u7167-2016-12-15-\u4e0b\u53481.23.03-291x300.png 291w\" sizes=\"auto, (max-width: 336px) 100vw, 336px\" \/><\/p>\n<p>\u8fd9\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u8ba1\u6570\u5668\u9875\u9762\uff0c\u5c31\u6709\u8fd9\u4e48\u591a\u590d\u6742\u7684\u6587\u4ef6\u548c\u8bbe\u5b9a\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6574\u4f53\u770b\u8d77\u6765\u53d8\u5f97\u66f4\u52a0\u590d\u6742\u4e86\u3002\u8fd9\u4e5f\u662f\u8bb8\u591aangular\u9635\u8425\u7684\u5f00\u53d1\u8005\u8bdf\u75c5react\u7684\u9ed1\u70b9\u4e4b\u4e00\u3002\u5f53\u7136\uff0c\u6211\u8fd9\u91cc\u5e76\u4e0d\u60f3\u8ba8\u8bbareact\u548cangular\u5b70\u4f18\u5b70\u52a3\u7684\u95ee\u9898\uff0c\u4e5f\u4e0d\u60f3\u5f15\u6218\uff0c\u8fd9\u91cc\u4ec5\u4ec5\u5c31\u4e2a\u4eba\u5f00\u53d1\u4f53\u9a8c\u5410\u69fd\u5410\u69fd\u800c\u5df2\u3002<\/p>\n<p>\u4e0b\u9762\u8fdb\u5165\u6b63\u9898\uff0c\u9996\u5148\uff0c\u5bf9Provider\u7684\u7406\u89e3\uff0c\u4e00\u4e2a\u901a\u5e38\u7684react+redux\u9879\u76ee\uff0c\u901a\u5e38\u4f1a\u5728app\u7684\u6700\u5916\u5c42\u7528Provider\u518d\u5305\u4e00\u5c42\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ config app root\r\nconst history = createHistory()\r\nconst root = (\r\n  &lt;Provider store={store} key=&quot;provider&quot;&gt;\r\n    &lt;Router history={history} routes={routes} \/&gt;\r\n  &lt;\/Provider&gt;\r\n)\r\n\r\n\/\/ render\r\nReactDOM.render(\r\n  root,\r\n  document.getElementById('root')\r\n)\r\n<\/pre>\n<p>\u800c\u5f53\u6211\u4eec\u4e0d\u4f7f\u7528redux\u7684\u65f6\u5019\uff0c\u53ea\u9700\u8981&lt;Route\/&gt;\u90a3\u4e2a\u7ec4\u4ef6\u5c31\u53ef\u4ee5\u4e86\uff0c\u8fd9\u4e00\u70b9\u5982\u4f55\u7406\u89e3\u5462\uff0c\u5b9e\u9645\u4e0a\u8fd9\u4e2a\u5e76\u4e0d\u96be\u7406\u89e3\uff0c\u5f00\u53d1\u8fc7react\u754c\u9762\u5c31\u4f1a\u77e5\u9053\uff0creact\u7684\u5de5\u4f5c\u539f\u7406\u5b9e\u9645\u4e0a\u662f\u7236\u7ec4\u4ef6\u5c06\u5c5e\u6027\uff0c\u4e5f\u5373property\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\uff0c\u800c\u6700\u7ec8\u5b50\u7ec4\u4ef6\u5c06\u81ea\u8eab\u7684property\u6e32\u67d3\u4e3a\u5b9e\u9645\u6d4f\u89c8\u5668\u663e\u793a\u7684\u5185\u5bb9\uff0c\u800c\u7ec4\u4ef6\u7684state\u5219\u662f\u4e0e\u7528\u6237\u64cd\u4f5c\u76f8\u5173\u7684\uff0c\u5f53state\u6539\u53d8\u65f6\uff0c\u6574\u4e2arender()\u65b9\u6cd5\u90fd\u4f1a\u91cd\u65b0\u6267\u884c\uff0c\u7136\u540e\u5c06\u65b0\u7684dom\u548c\u5f53\u524d\u663e\u793a\u7684dom\u5bf9\u6bd4\uff0c\u5c06\u5dee\u5f02\u7684\u90e8\u5206\u8fdb\u884c\u66f4\u65b0\u3002\u6240\u4ee5\u6211\u4eec\u4e0d\u4f7f\u7528redux\u7684\u65f6\u5019\uff0c\u901a\u5e38\u4f1a\u5c06\u7236\u7ec4\u4ef6\u7684state\u4f20\u5165\u5b50\u7ec4\u4ef6\uff0c\u4ee5\u5b8c\u6210\u6574\u4f53\u9875\u9762\u7684\u52a8\u4f5c\u66f4\u65b0\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n&lt;TextField style={styles.textField} \r\n           floatingLabelText=&quot;E-mail&quot; \r\n           value={this.state.email} \r\n           errorText={this.state.emailerr} \r\n           onChange={this.handleEmailChange} \/&gt;\r\n<\/pre>\n<p>TextField\u5c31\u662f\u4e00\u4e2a\u5b50\u7ec4\u4ef6\uff0c\u6211\u4eec\u5c06\u7236\u7ec4\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u4ee5\u53castate\u503c\u4f20\u5165\uff0c\u8fd9\u6837\u5f53\u7236\u7ec4\u4ef6\u7684state.email\u6539\u53d8\u65f6\uff0c\u5b50\u7ec4\u4ef6textField\u7684value\u4e5f\u4f1a\u7acb\u5373\u6539\u53d8\uff0c\u6539\u53d8\u4e5f\u4f1a\u663e\u793a\u5728\u5b9e\u9645\u7684UI\u754c\u9762\u4e0a\u3002\u6211\u4eec\u77e5\u9053React\u4e2d\u7684\u6570\u636e\u6d41\u662f\u5355\u5411\u4f20\u9012\u7684\uff0c\u53ea\u80fd\u7531\u7236\u7ec4\u4ef6\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\uff0c\u8fd9\u4e00\u70b9\u548cAngularJS\u4e0d\u540c\uff0cAngularJS\u7684\u6570\u636e\u6d41\u662f\u65e2\u53ef\u4ee5\u5355\u5411\u4e5f\u53ef\u4ee5\u53cc\u5411\u7ed1\u5b9a\u7684\u3002\u5355\u5411\u4f20\u9012\u8fd9\u4e2a\u7279\u6027\u867d\u7136\u7ed9\u6211\u4eec\u9020\u6210\u4e86\u4e00\u4e9b\u4e0d\u4fbf\uff0c\u4f46\u5374\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u6613\u8bfb\u66f4\u6613\u7ef4\u62a4\uff0c\u6570\u636e\u6d41\u4ee5\u53castate\u7684\u53d8\u5316\u66f4\u6613\u4e8e\u7406\u89e3\u3002\u6240\u4ee5\u901a\u5e38\u6765\u8bf4\uff0c\u6211\u4eec\u5728\u4e00\u4e2a\u5e94\u7528\u91cc\uff0c\u4f1a\u78b0\u5230\u7684\u6240\u6709\u60c5\u51b5\u53ef\u4ee5\u5f52\u7ed3\u4e3a\u4ee5\u4e0b\u4e09\u79cd\uff1a<\/p>\n<p>1.\u7236\u7ec4\u4ef6\u5411\u5b50\u7ec4\u4ef6\u4f20\u9012\u6570\u636e<\/p>\n<p>2.\u5b50\u7ec4\u4ef6\u5411\u7236\u7ec4\u4ef6\u4f20\u9012\u6570\u636e<\/p>\n<p>3.\u5b50\u7ec4\u4ef6\u548c\u5b50\u7ec4\u4ef6\u4e4b\u95f4\u4f20\u9012\u6570\u636e<\/p>\n<p>\u6211\u4eec\u77e5\u90531\u662f\u6700\u81ea\u7136\u7684\u60c5\u51b5\uff0creact\u539f\u751f\u5c31\u662f\u8fd9\u6837\u7684\u6570\u636e\u4f20\u9012\u6a21\u5f0f\uff0c\u800c2\u7684\u8bdd\uff0c\u53ef\u4ee5\u4f7f\u7528\u56de\u8c03\u51fd\u6570\u6765\u89e3\u51b3\u3002\u800c3\u662f\u6700\u9ebb\u70e6\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u8981\u89e3\u51b33\uff0c\u901a\u5e38\u4f1a\u9009\u62e9\u7528\u7236\u7ec4\u4ef6\u518d\u5c06\u8fd92\u4e2a\u5b50\u7ec4\u4ef6\u5305\u4e00\u4e0b\uff0c\u7136\u540e\u5b50\u7ec4\u4ef6\u5206\u522b\u5411\u7236\u7ec4\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u4f20\u9012\u6570\u636e\uff0c\u7136\u540e\u518d\u901a\u8fc7\u7236\u7ec4\u4ef6\u6839\u636e\u76f8\u5e94\u7684\u56de\u8c03\u51fd\u6570\u64cd\u4f5c\u6539\u53d8\u81ea\u8eab\u7684state\uff0c\u4f20\u9012\u7ed9\u76f8\u5e94\u7684\u5b50\u7ec4\u4ef6\uff0c\u8fd9\u6837\u5b50\u7ec4\u4ef6\u4e4b\u95f4\u5c31\u53ef\u4ee5\u4ea4\u4e92\u6570\u636e\u4e86\u3002\u867d\u7136\u8fd9\u6837\u4e5f\u80fd\u89e3\u51b3\u95ee\u9898\uff0c\u4f46\u662f\u4e0d\u540c\u4eba\u5199\u7684\u4ee3\u7801\u53c2\u5dee\u4e0d\u9f50\uff0c\u800c\u4e14\u82b1\u6837\u767e\u51fa\uff0c\u53ef\u8bfb\u6027\u4f1a\u5f88\u5dee\uff0c\u96be\u4ee5\u7ef4\u62a4\uff0c\u5728\u8fd9\u65f6\u5019\uff0c\u5c31\u8981\u8bf7\u51fa\u6211\u4eec\u7684redux\u4e86\uff0c\u5176\u5b9e\u4e5f\u4e0d\u6b62\u662fredux\uff0c\u7c7b\u4f3c\u7684\u6570\u636e\u6d41\u6846\u67b6\u8fd8\u6709flux\u548creflux\u4e5f\u80fd\u89e3\u51b3\u95ee\u9898\uff0c\u6846\u67b6\u7684\u76ee\u7684\u5176\u5b9e\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u53bb\u7ec4\u7ec7\u4ee3\u7801\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u65b9\u4fbf\u7ef4\u62a4\uff0c\u4ec5\u6b64\u800c\u5df2\u3002\u5982\u679c\u613f\u610f\u81ea\u5df1\u9020\u8f6e\u5b50\u89e3\u51b3\uff0c\u5c31\u50cf\u6211\u4e0a\u9762\u8bf4\u7684\uff0c\u7528\u7236\u7ec4\u4ef6\u53bb\u5305\u88f9\uff0c\u4e0d\u7528redux\u4e5f\u662f\u5b8c\u5168\u53ef\u4ee5\u7684\u3002redux\u53ea\u662f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u548c\u8bbe\u8ba1\u601d\u8def\u800c\u5df2\uff0c\u5176\u88ab\u8bbe\u8ba1\u7684\u76ee\u7684\u4e5f\u4e3b\u8981\u662f\u4e3a\u4e86\u89e3\u51b3\u4e0a\u9762\u7684\u7b2c3\u79cd\u60c5\u51b5\u7684\u3002<\/p>\n<p>\u8bf4\u5230\u8fd9\u91cc\uff0cProvider\u7684\u4f5c\u7528\u5c31\u663e\u800c\u6613\u89c1\u4e86\uff0c\u7531\u4e8eredux\u5168\u5c40\u53ea\u67091\u4e2astore\uff0c\u800c\u5728react\u4e2d\uff0c\u6570\u636e\u6d41\u53ea\u80fd\u7531\u7236\u7ec4\u4ef6\u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6\uff0c\u56e0\u6b64\uff0cProvider\u5c31\u662f\u8fd9\u6837\u4e00\u4e2a\u201c\u8d85\u7ea7\u7236\u7ec4\u4ef6\u201d\uff0c\u5b83\u662f\u6574\u4e2a\u6839\u8282\u70b9\u7684\u7236\u7ec4\u4ef6\uff0cstore\u5c31\u7531\u8fd9\u4e2a\u201c\u8d85\u7ea7\u7236\u7ec4\u4ef6\u201d\u5411\u6240\u6709\u5b50\u7ec4\u4ef6\u4f20\u9012\u3002\u6240\u4ee5\u4f60\u4f1a\u53d1\u73b0\uff0c\u4f7f\u7528\u4e86redux\u4e4b\u540e\uff0c\u6240\u6709\u7ec4\u4ef6\u90fd\u53ea\u9700\u8981\u8bbe\u5b9apropeties\u800c\u4e0d\u518d\u9700\u8981state\u4e86\u3002<\/p>\n<p>\u4e0b\u9762\u5c31\u662f\u4e00\u4e2a\u5178\u578b\u7684\u4f7f\u7528\u4e86redux\u4e4b\u540e\u5b50\u7ec4\u4ef6\u7684\u5199\u6cd5\uff1a<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport React,{Component,PropTypes} from 'react';\r\n\r\nclass Counter extends Component {\r\n    static propTypes = {\r\n        \/\/increment\u5fc5\u987b\u4e3afucntion,\u4e14\u5fc5\u987b\u5b58\u5728\r\n        increment: PropTypes.func.isRequired,\r\n        incrementIfOdd: PropTypes.func.isRequired,\r\n        incrementAsync: PropTypes.func.isRequired,\r\n        decrement: PropTypes.func.isRequired,\r\n        \/\/counter\u5fc5\u987b\u4e3a\u6570\u5b57\uff0c\u4e14\u5fc5\u987b\u5b58\u5728\r\n        counter: PropTypes.number.isRequired\r\n    };\r\n\r\n    render() {\r\n        \/\/\u4ece\u7ec4\u4ef6\u7684props\u5c5e\u6027\u4e2d\u5bfc\u5165\u56db\u4e2a\u65b9\u6cd5\u548c\u4e00\u4e2a\u53d8\u91cf\r\n        const { increment, incrementIfOdd, incrementAsync, decrement, counter } = this.props;\r\n        \/\/\u6e32\u67d3\u7ec4\u4ef6\uff0c\u5305\u62ec\u4e00\u4e2a\u6570\u5b57\uff0c\u56db\u4e2a\u6309\u94ae\r\n        return (\r\n            \r\n&lt;div&gt;\r\n\r\n\r\n                    Clicked: {counter} times\r\n                    {' '}\r\n                    &lt;button onClick={increment}&gt;+&lt;\/button&gt;\r\n                    {' '}\r\n                    &lt;button onClick={decrement}&gt;-&lt;\/button&gt;\r\n                    {' '}\r\n                    &lt;button onClick={incrementIfOdd}&gt;Increment if odd&lt;\/button&gt;\r\n                    {' '}\r\n                    &lt;button onClick={() =&gt; incrementAsync()}&gt;Increment async&lt;\/button&gt;\r\n                \r\n\r\n            &lt;\/div&gt;\r\n\r\n        );\r\n    }\r\n}\r\n\r\nexport default Counter;\r\n<\/pre>\n<p>\u5176\u4e2d\uff0c\u6e32\u67d3\u7ec4\u4ef6\u7684\u72b6\u6001\u4f7f\u7528\u7684\u662fprops\u53bb\u6e32\u67d3\u7684\uff0c\u56e0\u4e3a\u8fd9\u65f6\u5019\u6240\u6709\u5b50\u7ec4\u4ef6\u7684properties\uff0c\u90fd\u88abProvider\u6240\u4f20\u9012\u7684store\u4e2d\u7684state\u6240\u8bbe\u5b9a\u4e86\uff0c\u6240\u4ee5\u53ea\u9700\u8981properties\u5c31\u53ef\u4ee5\u4e86\uff0c\u800c\u4e24\u4e2a\u7c7b\u578b\u662ffunc\u7684\u5c5e\u6027\uff0c\u5176\u5b9e\u5c31\u662f\u56de\u8c03\u51fd\u6570\u3002\u7136\u800c\uff0c\u6b63\u662f\u56e0\u4e3a\u5355\u5411\u6570\u636e\u6d41\u7684\u5173\u7cfb\uff0c\u5f53\u5b50\u7ec4\u4ef6\u5c42\u6570\u975e\u5e38\u6df1\u7684\u65f6\u5019\uff0c\u6b64\u65f6\u5728\u6bcf\u4e2a\u7ec4\u4ef6\u91cc\u90fd\u52a0\u5165\u4e00\u4e2a\u7528\u6765\u4f20\u9012store\u7684\u5c5e\u6027\uff0c\u96be\u514d\u8fd9\u79cd\u5199\u6cd5\u6709\u70b9\u592a\u5197\u4f59\u4e86\u3002\u6240\u4ee5\uff0c\u8fd9\u65f6\u5019\u5c31\u8981\u8bf7\u51fa\u6211\u4eec\u7684connect\u65b9\u6cd5\u4e86\u3002connect\u65b9\u6cd5\u7684\u76ee\u7684\u5c31\u662f\u5c06\u5b50\u7ec4\u4ef6\u548cstore\u76f4\u63a5connect\u5728\u4e00\u8d77\uff0c\u800c\u4e0d\u7528\u6bcf\u4e00\u5c42\u90fd\u53bb\u4f20\u9012\u8fd9\u4e2astore\u4e86\uff0c\u5b9e\u9645\u4e5f\u76f8\u5f53\u4e8e\u4e00\u4e2a\u8bed\u6cd5\u7cd6\u7684\u4f5c\u7528\u3002<\/p>\n<p>\u4e8e\u662f\uff0c\u4f7f\u7528\u4e86redux\u4e4b\u540e\uff0c\u5b50\u7ec4\u4ef6\u901a\u5e38\u4f1a\u7528\u4e0b\u9762\u8fd9\u6837\u7684\u4e00\u4e2aContainer\u5305\u8d77\u6765\uff1a<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nimport {bindActionCreators} from 'redux';\r\nimport {connect} from 'react-redux';\r\nimport Counter from '..\/components\/Counter';\r\nimport * as CounterActions from '..\/actions\/counter';\r\n\r\n\r\nfunction mapStateToProps(state) {\r\n    return {\r\n        counter: state.counter,\r\n    }\r\n}\r\n\r\nfunction mapDispatchToProps(dispatch) {\r\n    return bindActionCreators(CounterActions,dispatch);\r\n}\r\n\r\n\r\nexport default connect(mapStateToProps,mapDispatchToProps)(Counter);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5176\u4e2d\uff0cconnect\u65b9\u6cd5\u76842\u4e2a\u53c2\u6570\u5206\u522b\u662fmapStateToProps\u548cmapDispatchToProps\uff0c\u7136\u540e\u6700\u540e\u4f20\u5165\u6211\u4eec\u7684Counter\u7ec4\u4ef6\u3002\u8fd9\u65f6\u5019\uff0c\u5c31\u5f88\u5bb9\u6613\u7406\u89e3\u4e86\uff0cmapStateToProps\u7684\u4f5c\u7528\u5c31\u5728\u4e8e\u4ecestore\u4e2d\u53d6\u51fa\u6e32\u67d3\u8fd9\u4e2a\u7ec4\u4ef6\u6240\u9700\u8981\u7684state\uff0c\u5c06\u5176\u6ce8\u5165\u8be5\u5b50\u7ec4\u4ef6\u7684Props\uff0c\u800credux\u4e2d\u7684Dispatch\u4f5c\u7528\u5c31\u662f\u53d1\u8d77\u4e00\u4e2aaction\u8ba9reducer\u5904\u7406\uff0c\u5c31\u76f8\u5f53\u4e8e\u524d\u9762\u63d0\u5230\u7684\uff0c\u7236\u7ec4\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u7684\u4f5c\u7528\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0cmapDispatchToProps\u7684\u4f5c\u7528\u5c31\u5728\u4e8e\u5c06\u7236\u7ec4\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u4e5f\u4f5c\u4e3aProps\u4f20\u5165\u5b50\u7ec4\u4ef6\u3002\u8fd9\u5c31\u662f\u6211\u524d\u9762\u6240\u8ff0\u7684\u201c\u5b50\u7ec4\u4ef6\u901a\u8fc7\u7236\u7ec4\u4ef6\u56de\u8c03\u51fd\u6570\u6539\u53d8\u7236\u7ec4\u4ef6\u72b6\u6001\uff0c\u8fdb\u800c\u6539\u53d8\u81ea\u8eab\u6216\u8005\u5176\u4ed6\u5b50\u7ec4\u4ef6Props\u6765\u5b9e\u73b0\u5b50\u7ec4\u4ef6\u95f4\u7684\u6570\u636e\u4f20\u9012\u201d\u8fd9\u4e2a\u5b9e\u73b0\u601d\u8def\u7684\u4e00\u79cd\u5b9e\u73b0\u800c\u5df2\uff0c\u5230\u73b0\u5728\uff0c\u5e94\u8be5\u5bf9\u4e8ereact-redux\u6bcf\u4e2a\u64cd\u4f5c\u7684\u610f\u4e49\u5c31\u5f88\u5bb9\u6613\u7406\u89e3\u4e86\u3002<\/p>\n<p>\u81f3\u4e8eaction\u548creducer\uff0c\u5b9e\u9645\u4e0a\u662f\u4e00\u79cd\u56de\u8c03\u51fd\u6570\u7684\u7ec4\u7ec7\u5f62\u5f0f\u800c\u5df2\uff0c\u6bcf\u4e2aaction\u53d1\u51fa\u7684dispatch\u7528\u4e8e\u533a\u5206\u8fd9\u4e2a\u64cd\u4f5c\u8981\u5b8c\u6210\u4ec0\u4e48\uff0c\u800creducer\u6839\u636e\u5177\u4f53\u8981\u5b8c\u6210\u7684\u5de5\u4f5c\u6765\u6539\u53d8store\u91cc\u7684\u5185\u5bb9\uff0c\u8fd9\u6837\u7684\u8bdd\uff0c\u5c31\u53ef\u4ee5\u66f4\u6e05\u6670\u7684\u53bb\u7ec4\u7ec7\u56de\u8c03\u51fd\u6570(\u5728\u8fd9\u91cc\u53ebreducer)\uff0c\u800c\u4e0d\u5fc5\u5bf9\u4e8e\u6bcf\u4e2a\u529f\u80fd\uff0c\u90fd\u81ea\u5df1\u5728\u7236\u7ec4\u4ef6\u4e2d\u81ea\u5df1\u547d\u540d\u4e00\u4e2a\u8fc7\u51e0\u5929\u5c31\u5fd8\u4e86\u8981\u5e72\u5565\u7684\u56de\u8c03\u51fd\u6570\uff0c\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u7528action+dispatch+reducer\u7684\u6a21\u5f0f\u53bb\u7ec4\u7ec7\u4ee3\u7801\u663e\u7136\u6bd4\u81ea\u5df1\u968f\u4fbf\u547d\u540d\u56de\u8c03\u51fd\u6570\u8981\u6709\u7ae0\u6cd5\u7684\u591a\u3002<\/p>\n<p>\u81f3\u4e8e\u5176\u4ed6middleware\u7684\u529f\u80fd\uff0c\u4e3b\u8981\u662f\u4e3a\u4e86\u89e3\u51b3\u4e00\u4e9b\u7ec6\u8282\u65b9\u9762\u7684\u5904\u7406\uff0c\u6bd4\u5982redux-thunk\u7528\u4e8e\u89e3\u51b3\u5f02\u6b65action\u7684\u95ee\u9898\uff0c\u8fd8\u6709\u4e00\u4e9b\u5c06reducer\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u529f\u80fd\u7b49\u7b49\uff0c\u8fd9\u4e9b\u90fd\u5c5e\u4e8e\u7ec6\u8282\u65b9\u9762\u7684\u5b9e\u73b0\uff0c\u5176\u5b9e\u601d\u8def\u8fd8\u662f\u975e\u5e38\u7b80\u5355\u660e\u4e86\u7684\u3002<\/p>\n<p>\u6700\u540e\u8bf4\u8bf4\u7ed3\u8bba\u5427\uff0c\u8bf4\u767d\u4e86react-redux\u5c31\u662f\u5b9e\u73b0\u4e86\u5c06\u6240\u6709\u5b50\u7ec4\u4ef6\u7528\u8d85\u7ea7\u7236\u7ec4\u4ef6\u5305\u88f9\uff0c\u7136\u540e\u901a\u8fc7\u56de\u8c03\u51fd\u6570\u89e3\u51b3\u5355\u5411\u6570\u636e\u6d41\u4e2d\u5b50\u7ec4\u4ef6\u4e4b\u95f4\u7684\u8026\u5408\u95ee\u9898\uff0c\u5e76\u7528\u66f4\u4f18\u96c5\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u7684\u65b9\u5f0f\u5c06\u4ee3\u7801\u7ec4\u7ec7\u8d77\u6765\uff0c\u8fd9\u5c31\u662freact-redux\u7684\u7cbe\u9ad3\u6240\u5728\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u7b14\u8005\u6c34\u5e73\u6709\u9650\uff0c\u672c\u6587\u4e5f\u4ec5\u4ec5\u5904\u4e8e\u7b14\u8005\u4e2a\u4eba\u5f00\u53d1\u4f53\u4f1a\u548c\u89c2\u70b9\uff0c\u6709\u4efb\u4f55\u5176\u4ed6\u89c2\u70b9\u6216\u8005\u672c\u6587\u4e2d\u6709\u4efb\u4f55\u758f\u6f0f\u6216\u9519\u8bef\u4e4b\u5904\uff0c\u4e5f\u6b22\u8fce\u4e00\u8d77\u63a2\u8ba8\u3002<\/p>\n<p>\u5173\u4e8e\u6587\u4e2d\u793a\u4f8b\u9879\u76ee\u7684\u5b8c\u6574\u4ee3\u7801\uff0c\u5927\u5bb6\u53ef\u4ee5\u5728<a href=\"https:\/\/www.jarvisw.com\/wp-content\/uploads\/2016\/12\/redux-test.zip\">\u8fd9\u91cc<\/a>\u4e0b\u8f7d\u3002<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u5728Jarvis OJ\u7684\u524d\u7aef\u5f00\u53d1\u7684\u65f6\u5019\uff0c\u4f7f\u7528\u4e86react+redux\u7684\u6570\u636e\u6d41\u7ec4\u7ec7\u5f62\u5f0f\uff0c\u7531\u4e8eredux\u53ea\u662f\u4e00\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[34,35,28,32,36],"class_list":["post-134","post","type-post","status-publish","format-standard","hentry","category-web","tag-react","tag-redux","tag-web","tag-32","tag-36"],"_links":{"self":[{"href":"https:\/\/www.jarvisw.com\/index.php?rest_route=\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jarvisw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jarvisw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jarvisw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jarvisw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=134"}],"version-history":[{"count":10,"href":"https:\/\/www.jarvisw.com\/index.php?rest_route=\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/www.jarvisw.com\/index.php?rest_route=\/wp\/v2\/posts\/134\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/www.jarvisw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jarvisw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jarvisw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}