{"id":1519,"date":"2021-07-18T19:15:29","date_gmt":"2021-07-18T11:15:29","guid":{"rendered":"https:\/\/www.7gugu.com\/?p=1519"},"modified":"2024-08-18T01:09:42","modified_gmt":"2024-08-17T17:09:42","slug":"mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/7gugu.com\/index.php\/2021\/07\/18\/mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e7%9a%84%e5%8c%ba%e5%88%ab\/","title":{"rendered":"mapStateToProps \u548c mapDispatchToProps\u7684\u533a\u522b"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u80cc\u666f<\/h2>\n\n\n\n<p>\u6700\u8fd1\u5f00\u59cb\u5b9e\u4e60\uff0c\u90e8\u95e8\u4e3b\u8981\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u5185\u90e8\u7684\u8de8\u7aef\u6846\u67b6\uff0c\u8fd9\u4e1c\u897f\u672c\u8d28\u4e0a\u8fd8\u662fReact Native\uff0c\u53ea\u4e0d\u8fc7\u662f\u505a\u4e86\u4e00\u5c42\u5957\u5a03\u91cd\u547d\u540d\u4e86\u6807\u7b7e\u540d\u79f0(\u50cf\u6781\u4e86ReactRouter\u548cReactRouter-Dom\u7684\u5173\u7cfb)\uff0c\u5e76\u4e14\u5c01\u88c5\ud83d\udce6\u4e86\u81ea\u5df1\u7684\u4e00\u5806\u79c1\u6709\u7684API\u3002\u7531\u4e8e\u5bfc\u5e08\u5c31\u662f\u8d1f\u8d23\u5f00\u53d1\u8fd9\u4e2a\u6846\u67b6\uff0c\u56e0\u6b64\u5f88\u65e0\u5948\u53ea\u80fd\u8f6cReact\uff0c\u505aReact\u5f00\u53d1\u4e86(\u5370\u8bc1\u4e86\u4e4b\u524d\u7684\u731c\u60f3)\u3002<\/p>\n\n\n\n<p>\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u5c31\u5206\u4eab\u4e00\u4e0b\u6211\u5728\u4f7f\u7528Redux\u8fc7\u7a0b\u4e2d\u7684\u4e00\u4e9b\u7b14\u8bb0\ud83d\udcd2\u6216\u8005\u4f7f\u7528\u7ecf\u9a8c\u3002<\/p>\n\n\n\n<p><strong>(\u672c\u6587\u7ae0\u4e0d\u4f1a\u8be6\u7ec6\u9610\u8ff0\u5176\u5177\u4f53\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u53ea\u662f\u4f1a\u8bb0\u5f55\u4e00\u4e9b\u5b9e\u9645\u5e94\u7528\u7684\u6548\u679c)<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u533a\u522b<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u76f8\u540c\u70b9<\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e24\u8005\u90fd\u662f\u51fd\u6570<\/li>\n\n\n\n<li>\u7528\u4e8e\u5c06Redux\u7684Store\u6620\u5c04\u5230\u7ec4\u4ef6props(\u5165\u53e3\u53c2\u6570)\u4e0a<\/li>\n<\/ul>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u5f02\u540c\u70b9<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u51fd\u6570\u540d\u79f0<\/th><th>\u5b98\u65b9\u63cf\u8ff0<\/th><th>\u4e2a\u4eba\u7406\u89e3<\/th><\/tr><\/thead><tbody><tr><td>mapStateToProps<\/td><td>\u5efa\u7acb\u4e00\u4e2a\u4ece(\u5916\u90e8\u7684)<code>state<\/code>\u5bf9\u8c61\u5230(UI \u7ec4\u4ef6\u7684)<code>props<\/code>\u5bf9\u8c61\u7684\u6620\u5c04\u5173\u7cfb<\/td><td>\u5c06Store\u4e0a\u7684State\u6302\u8f7d\u5230props\u4e0a (\u5b9e\u73b0\u4e86\u5355\u5411\u6570\u636e\u6d41)<\/td><\/tr><tr><td>mapDispatchToProps<\/td><td>\u5efa\u7acb UI \u7ec4\u4ef6\u7684\u53c2\u6570\u5230<code>store.dispatch<\/code>\u65b9\u6cd5\u7684\u6620\u5c04<\/td><td>\u5c06Store\u4e0a\u7684Dispatch\u51fd\u6570\u6302\u8f7d\u5230(\u8c03\u7528\u7684\u65f6\u5019\uff0c\u76f8\u5f53\u4e8e\u662f\u5728\u89e6\u53d1Store\u4e0a\u7684\u51fd\u6570)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b9e\u4f8b<\/h2>\n\n\n\n<p>(\u6b64\u5904\u5f15\u7528\u962e\u4e00\u5cf0\u8001\u5e08\u63d0\u4f9b\u7684\u201c\u8ba1\u6570\u5668\u201d\u4ee3\u7801\uff0c\u4f5c\u4e3a\u672c\u6587\u7ae0\u7684\u4f8b\u5b50\uff0c\u611f\u8c22\u962e\u8001\u5e08\u7684\u5206\u4eab\ud83d\ude4f)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u521d\u59cb\u5316\u73af\u5883<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;import React, { Component } from 'react'<br>&nbsp;import ReactDOM from 'react-dom'<br>&nbsp;import { createStore } from 'redux'<br>&nbsp;import { Provider, connect } from 'react-redux'<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efa<code>\u8ba1\u6570\u5668<\/code>\u7ec4\u4ef6<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;class Counter extends Component {<br>&nbsp; &nbsp;\/\/ \u521b\u5efa\u6e32\u67d3\u5668<br>&nbsp; &nbsp;render() {<br>&nbsp; &nbsp; &nbsp;\/\/ \u4ece\u5165\u53e3\u53c2\u6570\u4e0a\u83b7\u53d6mapState\u548cmapDispatch\u6302\u8f7d\u7684\u5c5e\u6027\u548c\u65b9\u6cd5<br>&nbsp; &nbsp; &nbsp;const { value, onIncreaseClick } = this.props<br>&nbsp; &nbsp; &nbsp;\/\/ \u8fd4\u56deJSX<br>&nbsp; &nbsp; &nbsp;return (<br>&nbsp; &nbsp; &nbsp; &nbsp;&lt;div&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;span&gt;{value}&lt;\/span&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;button onClick={onIncreaseClick}&gt;Increase&lt;\/button&gt;<br>&nbsp; &nbsp; &nbsp; &nbsp;&lt;\/div&gt;<br>&nbsp; &nbsp;  )<br>&nbsp;  }<br>&nbsp;}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efaAction<\/h3>\n\n\n\n<p>Action\u8bf4\u660e\u4e86Redux\u4e2d<strong>\u53ef\u4ee5\u505a<\/strong>\u4ec0\u4e48\u52a8\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;const increaseAction = { type: 'increase' }<\/pre>\n\n\n\n<p>\u6709\u70b9\u7c7b\u4f3c\u4e8e<code>Java<\/code>\u4e2d\u7684\u62bd\u8c61\u7c7b\u7684\u6982\u5ff5<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efaReducer<\/h3>\n\n\n\n<p>Reducer\u8bf4\u660e\u4e86Action<strong>\u505a\u4ec0\u4e48<\/strong>\u52a8\u4f5c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;function counter(state = { count: 0 }, action) {<br>&nbsp; &nbsp;\/\/ \u8bfb\u53d6Redux\u4e2d\u7ba1\u7406\u7684count\u53d8\u91cf<br>&nbsp; &nbsp;const count = state.count<br>&nbsp; &nbsp;\/\/ \u68c0\u6d4bdispatch\u89e6\u53d1\u4e86\u54ea\u4e00\u4e2a\u52a8\u4f5c<br>&nbsp; &nbsp;switch (action.type) {<br>&nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u89e6\u53d1\"\u589e\u52a0\"\u52a8\u4f5c<br>&nbsp; &nbsp; &nbsp;case 'increase':<br>&nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5c06\u503c+1\u5e76\u8fd4\u56de\u4e00\u4e2astate\u5230Redux\u7684Store\u4e2d<br>&nbsp; &nbsp; &nbsp; &nbsp;return { count: count + 1 }<br>&nbsp; &nbsp; &nbsp;default:<br>&nbsp; &nbsp; &nbsp; &nbsp;return state<br>&nbsp;  }<br>&nbsp;}<\/pre>\n\n\n\n<p>\u6709\u70b9\u7c7b\u4f3c\u4e8e<code>Java<\/code>\u4e2d\u7684\u5b9e\u73b0\u7c7b<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">PS:<\/h4>\n\n\n\n<p>Action\u548cReducer\u7684\u5173\u7cfb\uff0c\u5728\u6211\u770b\u6765\u6709\u70b9\u7c7b\u4f3c\u4e8e\u62bd\u8c61\u5b9e\u73b0\u7684\u5173\u7cfb\uff0c\u4e0d\u77e5\u9053\ud83e\udd37\u200d\u2642\ufe0f\u5bf9\u4e0d\u5bf9\uff0c\u4ee5\u540e\u518d\u7ee7\u7eed\u6df1\u5165\u63a2\u7a76\u4e00\u4e0b\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efaStore<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;const store = createStore(counter)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9e\u73b0\u6302\u8f7d\u51fd\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\/\/ Redux\u7684\u89d2\u5ea6: \u5c06Redux\u7ba1\u7406\u7684\u5168\u5c40State(Store)\u4e0a\u7684\u503c\u6302\u8f7d\u5230\u4e86\u7ec4\u4ef6\u4e0a<br>&nbsp;\/\/ \u7ec4\u4ef6\u7684\u89d2\u5ea6: \u83b7\u53d6(\u8ba2\u9605)\u4e86Redux\u7ba1\u7406\u7684\u5168\u5c40State(Store)\u4e0a\u7684\u503c<br>&nbsp;function mapStateToProps(state) {<br>&nbsp; &nbsp;return {<br>&nbsp; &nbsp; &nbsp;value: state.count<br>&nbsp;  }<br>&nbsp;}<br>&nbsp;\u200b<br>&nbsp;\/\/ Redux\u7684\u89d2\u5ea6: \u5c06\u64cd\u4f5c\u5168\u5c40State(Store)\u4e0a\u7684\u65b9\u6cd5\u6302\u8f7d\u5230\u4e86\u7ec4\u4ef6\u4e0a<br>&nbsp;\/\/ \u7ec4\u4ef6\u7684\u89d2\u5ea6: \u83b7\u53d6\u4e86\u7ba1\u7406\u7684\u5168\u5c40State(Store)\u7684\u65b9\u6cd5<br>&nbsp;function mapDispatchToProps(dispatch) {<br>&nbsp; &nbsp;return {<br>&nbsp; &nbsp; &nbsp;onIncreaseClick: () =&gt; dispatch(increaseAction)<br>&nbsp;  }<br>&nbsp;}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6302\u8f7d\u5230\u7ec4\u4ef6\u4e0a<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;\/\/ Connected Component<br>&nbsp;const App = connect(<br>&nbsp; &nbsp;mapStateToProps,<br>&nbsp; &nbsp;mapDispatchToProps<br>&nbsp;)(Counter)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u9875\u9762\u6e32\u67d3<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">&nbsp;ReactDOM.render(<br>&nbsp; &nbsp;&lt;Provider store={store}&gt;<br>&nbsp; &nbsp; &nbsp;&lt;App \/&gt;<br>&nbsp; &nbsp;&lt;\/Provider&gt;,<br>&nbsp; &nbsp;document.getElementById('root')<br>&nbsp;)<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6d41\u7a0b\u56fe\u4f8b<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-dominant-color=\"f6f6f6\" data-has-transparency=\"false\" style=\"--dominant-color: #f6f6f6;\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"946\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" src=\"https:\/\/114.132.121.208\/wp-content\/uploads\/2021\/07\/image-2.png\" alt=\"\" class=\"wp-image-3854 not-transparent\" srcset=\"https:\/\/7gugu.com\/wp-content\/uploads\/2021\/07\/image-2.png 1024w, https:\/\/7gugu.com\/wp-content\/uploads\/2021\/07\/image-2-300x277.png 300w, https:\/\/7gugu.com\/wp-content\/uploads\/2021\/07\/image-2-768x710.png 768w\" \/><figcaption class=\"wp-element-caption\">Redux\u4e0e\u7ec4\u4ef6\u4e4b\u95f4\u662f\u5982\u4f55\u4f20\u9012reducer\u548cstate\u4ee5\u53ca\u7ec4\u4ef6\u5982\u4f55dispatch reducer\u7684<\/figcaption><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7<code>mapStateToProps<\/code>\u5c06\u5168\u5c40State\u6302\u8f7d\u5230\u4e86\u7ec4\u4ef6\u7684Props\u4e0a\uff0c\u7ec4\u4ef6\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6\u5230Store\u4e2d\u7ba1\u7406\u7684state\u503c\u3002<\/li>\n\n\n\n<li>\u901a\u8fc7<code>mapDispatchToProps<\/code>\u5c06\u7ba1\u7406\u5168\u5c40State\u7684\u65b9\u6cd5\u6302\u8f7d\u5230\u4e86\u7ec4\u4ef6\u7684Props\u4e0a\uff0c\u7ec4\u4ef6\u5c31\u53ef\u4ee5\u4ece\u5165\u53c2\u4e2d\u83b7\u53d6\u64cd\u4f5c\u65b9\u6cd5\uff0c\u8fdb\u800c\u5728\u7ec4\u4ef6\u4e2ddispatch(\u89e6\u53d1)State\u4e2d\u7684Reducer\u8fdb\u800c\u4fee\u6539State\u4e2d\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ed3\u5c3e<\/h2>\n\n\n\n<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u68b3\u7406\u4e86\u4e00\u4e0b\u672c\u5468\u89e3\u51b3\u7684\u4e00\u4e2a\u77e5\u8bc6\u70b9\uff0c\u52a0\u6df1\u81ea\u79c1\u70b9\u5728\u8111\u6d77\u4e2d\u7684\u5370\u8c61\u3002\u6587\u7ae0\u4e2d\u9610\u8ff0\u4e0d\u6e05\u695a\u7684\u70b9\uff0c\u6b22\u8fce\u900f\u8fc7\u90ae\u7bb1\ud83d\udcee\u4e0e\u53d1\u8d77\u8ba8\u8bba\uff0c\u5e0c\u671b\u900f\u8fc7\u8ba8\u8bba\u4e5f\u80fd\u8ba9\u4f60\u660e\u767d\u8fd9\u4e2a\u8fc7\u7a0b\u53d1\u751f\u4e86\u4ec0\u4e48\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5ef6\u4f38\u8d44\u6599<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.ruanyifeng.com\/blog\/2016\/09\/redux_tutorial_part_three_react-redux.html\">React-Redux \u7684\u7528\u6cd5(\u4e2d\u6587)<\/a><\/li>\n\n\n\n<li><a href=\"Extracting%20Data%20with%20%60mapStateToProps%60\">\u4f7f\u7528mapStateToProps\u5bfc\u51fa\u6570\u636e(\u5b98\u65b9\u82f1\u6587)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/react-redux.js.org\/using-react-redux\/connect-mapdispatch\">\u4f7f\u7528mapDispatchToProps\u89e6\u53d1Actions(\u5b98\u65b9\u82f1\u6587)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/jackielii\/simplest-redux-example\/blob\/master\/index.js\">\u8ba1\u7b97\u5668\u4ee3\u7801(Github)<\/a>PS:\u5efa\u8bae\u53ef\u4ee5\u5148\u4e0d\u770b\ud83d\ude48\u8fd9\u4e24\u4e2a\u51fd\u6570\u5165\u53c2\u7684\u63cf\u8ff0\uff0c\u56e0\u4e3a\u5f88\u5bb9\u6613\u628a\u4eba\u641e\u6655\ud83d\ude35\uff0c\u53ef\u4ee5\u7740\u91cd\u770b\u770b[1]\u4e2d\uff0c\u962e\u4e00\u5cf0\u8001\u5e08\u63d0\u4f9b\u7684\u5b9e\u4f8b\uff0c\u642d\u914d[2] [3]\u601d\u8003\ud83e\udd14\u4e00\u4e0b\uff0c\u76f8\u4fe1\u4f60\u5f88\u5feb\u4e5f\u80fdget\u5230\u8fd9\u4e2a\u51fd\u6570\u5728\u5e72\u4ec0\u4e48\uff0c\u6709\u4ec0\u4e48\u7528\ud83e\udd37\u200d\u2642\ufe0f\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u6700\u8fd1\u5f00\u59cb\u5b9e\u4e60\uff0c\u90e8\u95e8\u4e3b\u8981\u4f7f\u7528\u7684\u662f\u4e00\u4e2a\u5185\u90e8\u7684\u8de8\u7aef\u6846\u67b6\uff0c\u8fd9\u4e1c\u897f\u672c\u8d28\u4e0a\u8fd8\u662fReact Native\uff0c\u53ea\u4e0d\u8fc7\u662f\u505a\u4e86 &hellip; <a href=\"https:\/\/7gugu.com\/index.php\/2021\/07\/18\/mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e7%9a%84%e5%8c%ba%e5%88%ab\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cmapStateToProps \u548c mapDispatchToProps\u7684\u533a\u522b\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,8,13],"tags":[],"class_list":["post-1519","post","type-post","status-publish","format-standard","hentry","category-react","category-daily","category-13"],"_links":{"self":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts\/1519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/comments?post=1519"}],"version-history":[{"count":1,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts\/1519\/revisions"}],"predecessor-version":[{"id":3855,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/posts\/1519\/revisions\/3855"}],"wp:attachment":[{"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/media?parent=1519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/categories?post=1519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/7gugu.com\/index.php\/wp-json\/wp\/v2\/tags?post=1519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}