{"id":1812,"date":"2017-08-21T11:57:59","date_gmt":"2017-08-21T03:57:59","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=1812"},"modified":"2017-08-21T11:58:00","modified_gmt":"2017-08-21T03:58:00","slug":"%e9%98%bf%e9%87%8c-react-%e6%a1%86%e6%9e%b6-mirror-%e4%bd%bf%e7%94%a8%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=1812","title":{"rendered":"\u963f\u91cc React \u6846\u67b6 mirror \u4f7f\u7528\u6559\u7a0b"},"content":{"rendered":"<p>\n\t\u6700\u8fd1\u4e24\u5929\u963f\u91cc\u4e00\u76f4\u5360\u636e\u7740\u5934\u6761\uff0c\u4e00\u4e2a\u662f\u652f\u4ed8\u5b9d\u5c0f\u7a0b\u5e8f\uff0c\u4e00\u4e2a\u662f mirror \u7684\u53d1\u5e03\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u7684\u4ecb\u7ecd mirror \u7684\u539f\u7406\uff0c\u7279\u70b9\u548c\u4f7f\u7528\u3002\n<\/p>\n<p>\n\tMirror \u662f\u4e00\u6b3e\u57fa\u4e8e React\u3001Redux \u548c react-router \u7684\u524d\u7aef\u6846\u67b6\uff0c\u7b80\u6d01\u9ad8\u6548\u3001\u7075\u6d3b\u53ef\u9760\u3002\n<\/p>\n<p>\n\tMirror \u4f7f\u7528\u7684\u662freact-router 4.x\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"\u963f\u91cc mirror\" src=\"http:\/\/img.bbs.csdn.net\/upload\/201708\/21\/1503287782_144421.png\" style=\"width: 361px;height: 251px\" \/>\n<\/p>\n<p>\n\t\u65e2\u7136\u6709\u4e86 React \u963f\u91cc\u4e3a\u4ec0\u4e48\u53c8\u63a8\u51fa mirror \u5462\uff1f\n<\/p>\n<p>\n\t\u8fd9\u4e00\u5207\u4f30\u8ba1\u5f97\u4ece\u5c0f\u7a0b\u5e8f\u8bf4\u8d77\u3002\u652f\u4ed8\u5b9d\u4e5f\u63a8\u51fa\u4e86\u5c0f\u7a0b\u5e8f\uff0c\u4f46\u662f\u5e95\u5c42\u5b8c\u5168\u548c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e0d\u540c\u3002\u5e76\u4e14\u652f\u4ed8\u5b9d\u7684\u5c0f\u7a0b\u5e8f\u662f\u57fa\u4e8e React \u7684\u3002\u4f46\u662f React \u548c Redux \u7684\u4e00\u4e9b\u7f3a\u70b9\u5728\u652f\u4ed8\u5b9d\u5c0f\u7a0b\u5e8f\u4e0a\u5c06\u88ab\u653e\u5927\uff0c\u56e0\u6b64\u963f\u91cc\u63a8\u51fa\u4e86 mirror\u3002\n<\/p>\n<p>\n\t\u4e00\u4e2a\u5178\u578b\u7684 React\/Redux \u5e94\u7528\u770b\u8d77\u6765\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a\n<\/p>\n<ul>\n<li>\n\t\t\u4e00\u4e2a actions\/ \u76ee\u5f55\u7528\u6765\u624b\u52a8\u521b\u5efa\u6240\u6709\u7684 action type\uff08\u6216\u8005 action creator\uff09\uff1b\n\t<\/li>\n<li>\n\t\t\u4e00\u4e2a reducers\/ \u76ee\u5f55\u4ee5\u53ca\u65e0\u6570\u7684 switch \u6765\u6355\u83b7\u6240\u6709\u7684 action type\uff1b\n\t<\/li>\n<li>\n\t\t\u5fc5\u987b\u8981\u4f9d\u8d56 middleware \u624d\u80fd\u5904\u7406 \u5f02\u6b65 action\uff1b\n\t<\/li>\n<li>\n\t\t\u660e\u786e\u8c03\u7528 dispatch \u65b9\u6cd5\u6765 dispatch \u6240\u6709\u7684 action\uff1b\n\t<\/li>\n<li>\n\t\t\u624b\u52a8\u521b\u5efa history \u5bf9\u8c61\u5173\u8054\u8def\u7531\u7ec4\u4ef6\uff0c\u53ef\u80fd\u8fd8\u9700\u8981\u4e0e store \u540c\u6b65\uff1b\n\t<\/li>\n<li>\n\t\t\u8c03\u7528 history \u4e0a\u7684\u65b9\u6cd5\u6216\u8005 dispatch action \u6765\u624b\u52a8\u66f4\u65b0\u8def\u7531\uff1b\n\t<\/li>\n<\/ul>\n<p>\n\t\u4ece\u4e0a\u9762\u5c31\u53ef\u4ee5\u770b\u51fa React\/Redux \u5b58\u5728\u7684\u95ee\u9898\u3002\u592a\u591a\u7684 <a href=\"https:\/\/github.com\/reactjs\/redux\/blob\/master\/docs\/recipes\/ReducingBoilerplate.md\" name=\"ReducingBoilerplate\" target=\"_blank\" title=\"ReducingBoilerplate\">\u6837\u677f\u6587\u4ef6<\/a> \u4ee5\u53ca\u7e41\u7410\u751a\u81f3\u91cd\u590d\u7684\u52b3\u52a8\u3002\n<\/p>\n<p>\n\t\u5b9e\u9645\u4e0a\uff0c\u4e0a\u8ff0\u5927\u90e8\u5206\u64cd\u4f5c\u90fd\u662f\u53ef\u4ee5\u7b80\u5316\u7684\u3002\u6bd4\u5982\uff0c\u5728\u5355\u4e2a API \u4e2d\u521b\u5efa\u6240\u6709\u7684 action \u548c reducer\uff1b\u6bd4\u5982\uff0c\u7b80\u5355\u5730\u8c03\u7528\u4e00\u4e2a\u51fd\u6570\u6765 dispatch \u6240\u6709\u7684\u540c\u6b65\u548c\u5f02\u6b65 action\uff0c\u4e14\u4e0d\u9700\u8981\u989d\u5916\u5f15\u5165 middleware\uff1b\u518d\u6bd4\u5982\uff0c\u4f7f\u7528\u8def\u7531\u7684\u65f6\u5019\u53ea\u9700\u8981\u5173\u5fc3\u5b9a\u4e49\u5177\u4f53\u7684\u8def\u7531\uff0c\u4e0d\u7528\u53bb\u5173\u5fc3 history \u5bf9\u8c61\uff0c\u7b49\u7b49\u3002\n<\/p>\n<p>\n\t\u8fd9\u6b63\u662f Mirror \u7684\u4f7f\u547d\uff0c\u7528\u6781\u5c11\u6570\u7684 API \u5c01\u88c5\u6240\u6709\u7e41\u7410\u751a\u81f3\u91cd\u590d\u7684\u5de5\u4f5c\uff0c\u63d0\u4f9b\u4e00\u79cd\u7b80\u6d01\u9ad8\u6548\u7684\u66f4\u9ad8\u7ea7\u62bd\u8c61\uff0c\u540c\u65f6\u4fdd\u6301\u539f\u6709\u7684\u5f00\u53d1\u6a21\u5f0f\u3002\n<\/p>\n<h2>\n\tMirror \u7684\u7279\u70b9<br \/>\n<\/h2>\n<ul>\n<li>\n\t\t\u6781\u7b80 API\uff08\u53ea\u6709 4 \u4e2a\u65b0 API\uff09\n\t<\/li>\n<li>\n\t\t\u6613\u4e8e\u4e0a\u624b\n\t<\/li>\n<li>\n\t\tRedux action \u4ece\u672a\u5982\u6b64\u7b80\u5355\n\t<\/li>\n<li>\n\t\t\u652f\u6301\u52a8\u6001\u521b\u5efa model\n\t<\/li>\n<li>\n\t\t\u5f3a\u5927\u7684 hook \u673a\u5236\n\t<\/li>\n<\/ul>\n<h2>\n\tMirror \u7684\u7528\u6cd5<br \/>\n<\/h2>\n<p>\n\t\u4f7f\u7528 <a href=\"https:\/\/github.com\/facebookincubator\/create-react-app\" name=\"create react app\" target=\"_blank\" title=\"create react app\">create-react-app<\/a> \u521b\u5efa\u4e00\u4e2a\u65b0\u7684 app\uff1a\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ npm i -g create-react-app\r\n$ create-react-app my-app<\/pre>\n<p>\n\t\u521b\u5efa\u4e4b\u540e\uff0c\u4ece npm \u5b89\u88c5 Mirror\uff1a\n<\/p>\n<pre class=\"brush:bash;\">\r\n$ cd my-app\r\n$ npm i --save mirrorx\r\n$ npm start<\/pre>\n<p>\n\t\u4e0b\u9762\u662f\u4f7f\u7528 mirror \u5bf9 action \u7684\u7528\u6cd5\uff0cindex.js \u4ee3\u7801\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:jscript;\">\r\nimport React from &#039;react&#039;\r\nimport mirror, {actions, connect, render} from &#039;mirrorx&#039;\r\n\r\n\/\/ \u58f0\u660e Redux state, reducer \u548c action\uff0c\r\n\/\/ \u6240\u6709\u7684 action \u90fd\u4f1a\u4ee5\u76f8\u540c\u540d\u79f0\u8d4b\u503c\u5230\u5168\u5c40\u7684 actions \u5bf9\u8c61\u4e0a\r\nmirror.model({\r\n  name: &#039;app&#039;,\r\n  initialState: 0,\r\n  reducers: {\r\n    increment(state) { return state + 1 },\r\n    decrement(state) { return state - 1 }\r\n  },\r\n  effects: {\r\n    async incrementAsync() {\r\n      await new Promise((resolve, reject) =&gt; {\r\n        setTimeout(() =&gt; {\r\n          resolve()\r\n        }, 1000)\r\n      })\r\n      actions.app.increment()\r\n    }\r\n  }\r\n})\r\n\r\n\/\/ \u4f7f\u7528 react-redux \u7684 connect \u65b9\u6cd5\uff0c\u8fde\u63a5 state \u548c\u7ec4\u4ef6\r\nconst App = connect(state =&gt; {\r\n  return {count: state.app}\r\n})(props =&gt; (\r\n    &lt;div&gt;\r\n      &lt;h1&gt;{props.count}&lt;\/h1&gt;\r\n      {\/* \u8c03\u7528 actions \u4e0a\u7684\u65b9\u6cd5\u6765 dispatch action *\/}\r\n      &lt;button onClick={() =&gt; actions.app.decrement()}&gt;-&lt;\/button&gt;\r\n      &lt;button onClick={() =&gt; actions.app.increment()}&gt;+&lt;\/button&gt;\r\n      {\/* dispatch async action *\/}\r\n      &lt;button onClick={() =&gt; actions.app.incrementAsync()}&gt;+ Async&lt;\/button&gt;\r\n    &lt;\/div&gt;\r\n  )\r\n)\r\n\r\n\/\/ \u542f\u52a8 app\uff0crender \u65b9\u6cd5\u662f\u52a0\u5f3a\u7248\u7684 ReactDOM.render\r\nrender(&lt;App\/&gt;, document.getElementById(&#039;root&#039;))<\/pre>\n<p>\n\t\u4ece\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u53ef\u4ee5\u770b\u51fa mirror \u7684\u4ee3\u7801\u5f88\u7b80\u6d01\uff0c\u5f88\u9ad8\u6548\u3002\u9664\u4e86\u4e0a\u9762\u8fd9\u4e9b&nbsp;mirror \u8fd8\u652f\u6301 <a href=\"https:\/\/github.com\/zalmoxisus\/redux-devtools-extension\" name=\"Redux DevTools extension\" target=\"_blank\" title=\"Redux DevTools extension\">Redux DevTools<\/a> \u6269\u5c55\u3002mirror&nbsp;\u4f7f\u7528\u989d\u5916\u7684 Redux middleware \u53ea\u9700\u8981\u5728mirror.defaults \u63a5\u53e3\u4e2d\u6307\u5b9a\u5373\u53ef\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=1812\">\u963f\u91cc React \u6846\u67b6 mirror \u4f7f\u7528\u6559\u7a0b<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u4e24\u5929\u963f\u91cc\u4e00\u76f4\u5360\u636e\u7740\u5934\u6761\uff0c\u4e00\u4e2a\u662f\u652f\u4ed8\u5b9d\u5c0f\u7a0b\u5e8f\uff0c\u4e00\u4e2a\u662f mirror \u7684\u53d1\u5e03\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u7684\u4ecb\u7ecd mirror  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[1441,373,1442],"class_list":["post-1812","post","type-post","status-publish","format-standard","hentry","category-html5","tag-mirror","tag-react","tag-redux"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1812"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1812\/revisions"}],"predecessor-version":[{"id":1813,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/1812\/revisions\/1813"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}