{"id":82,"date":"2019-05-03T03:51:15","date_gmt":"2019-05-03T03:51:15","guid":{"rendered":"https:\/\/www.369usa.com\/blog\/?p=82"},"modified":"2019-05-03T03:51:31","modified_gmt":"2019-05-03T03:51:31","slug":"react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90","status":"publish","type":"post","link":"https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/","title":{"rendered":"React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50"},"content":{"rendered":"<p>Source code Github location:<br \/>\n<a href=\"https:\/\/github.com\/howareyoucolin\/React-Redux-Counter\">https:\/\/github.com\/howareyoucolin\/React-Redux-Counter<\/a><\/p>\n<p>Reducer<\/p>\n<pre>const initialState = {\r\n\tcount: 10\r\n};\r\n\r\nfunction rootReducer(state = initialState, action) {\r\n\tif(action.type == 'INCREASE_COUNT'){\r\n\t\treturn {\r\n\t\t\t...state,\r\n\t\t\tcount: (state.count + 1) &gt; 20 ? 20 : state.count + 1\r\n\t\t}\r\n\t}\r\n\telse if(action.type == 'DECREASE_COUNT'){\r\n\t\treturn {\r\n\t\t\t...state,\r\n\t\t\tcount: (state.count - 1) &lt; 0 ? 0 : state.count - 1\r\n\t\t}\r\n\t}\r\n\treturn state;\r\n};\r\n\r\nexport default rootReducer;\r\n<\/pre>\n<p>mapStateToProps \u4f8b\u5b50:<\/p>\n<pre>import React from 'react';\r\nimport ReactDOM from 'react-dom';\r\nimport { connect } from 'react-redux';\r\n\r\n\/\/Import scss\r\nimport '.\/scss\/screen.scss';\r\n\r\n\/\/Component A\r\nclass Screen extends React.Component {\r\n\trender() {\r\n\t\treturn (\r\n\t\t\t&lt;div className=\"screen\"&gt;\r\n\t\t\t\t{ this.props.count }\r\n\t\t\t&lt;\/div&gt;\r\n\t\t)\r\n\t}\r\n};\r\n\r\nconst mapStateToProps = (state) =&gt; {\r\n\treturn { count: state.count };\r\n};\r\n\r\nexport default connect(mapStateToProps)(Screen);\r\n<\/pre>\n<p>mapDispatchToProps \u4f8b\u5b50:<\/p>\n<pre>import React from 'react';\r\nimport ReactDOM from 'react-dom';\r\nimport { connect } from 'react-redux';\r\n\r\n\/\/Import scss\r\nimport '.\/scss\/btn.scss';\r\n\r\n\/\/Component A\r\nclass Btn extends React.Component {\r\n\t\r\n\tclickHandler = () =&gt; {\r\n\t\tif(this.props.sign == '+') this.props.increment();\r\n\t\tif(this.props.sign == '-') this.props.decrement();\r\n\t}\r\n\t\r\n\trender() {\r\n\t\treturn (\r\n\t\t\t&lt;div className=\"btnWrap\"&gt;\r\n\t\t\t\t&lt;button onClick={this.clickHandler}&gt; {this.props.children} &lt;\/button&gt;\r\n\t\t\t&lt;\/div&gt;\r\n\t\t)\r\n\t}\r\n};\r\n\r\nconst mapDispatchToProps = (dispatch) =&gt; {\r\n\treturn {\r\n\t\tincrement:() =&gt; {\r\n\t\t\tdispatch({type:'INCREASE_COUNT'});\r\n\t\t},\r\n\t\tdecrement:() =&gt; {\r\n\t\t\tdispatch({type:'DECREASE_COUNT'});\r\n\t\t}\r\n\t}\r\n}\r\n\r\nexport default connect(null,mapDispatchToProps)(Btn);\r\n<\/pre>\n<p>Counter \u4f5c\u7528\u5c55\u793a:<br \/>\n<iframe style=\"width: 100%; height: 500px; border: 1px solid #000;\" src=\"https:\/\/www.369usa.com\/projects\/react-redux-counter\/\"><\/iframe><\/p>\n<p>\u7531369usa\u5b66\u751f\u9648\u5c0f\u5f1f\u6240\u7f16\u5199.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source code Github location: https:\/\/github.com\/howareyoucolin\/React-Redux-Counter Reducer const initialState = { count: 10 }; function rootReducer(state = initialState, action) { if(action.type == &#8216;INCREASE_COUNT&#8217;){ return { &#8230;state, count: (state.count + 1) &gt; 20 ? 20 : state.count + 1 } } else if(action.type == &#8216;DECREASE_COUNT&#8217;){ return { &#8230;state, count: (state.count &#8211; 1) &lt; 0 ? 0 : &hellip; <a href=\"https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50&#8221;<\/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":[5],"tags":[],"class_list":["post-82","post","type-post","status-publish","format-standard","hentry","category-5"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50 - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.369usa.com\/blog\/react-redux-counter\u4ee5\u53careducer-mapstatetoprops-\u548c-mapdispatchtoprops\u4f8b\u5b50\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50 - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"Source code Github location: https:\/\/github.com\/howareyoucolin\/React-Redux-Counter Reducer const initialState = { count: 10 }; function rootReducer(state = initialState, action) { if(action.type == &#039;INCREASE_COUNT&#039;){ return { ...state, count: (state.count + 1) &gt; 20 ? 20 : state.count + 1 } } else if(action.type == &#039;DECREASE_COUNT&#039;){ return { ...state, count: (state.count - 1) &lt; 0 ? 0 : &hellip; Continue reading &quot;React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.369usa.com\/blog\/react-redux-counter\u4ee5\u53careducer-mapstatetoprops-\u548c-mapdispatchtoprops\u4f8b\u5b50\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2019-05-03T03:51:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-03T03:51:31+00:00\" \/>\n<meta name=\"author\" content=\"darkersss\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"darkersss\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/\",\"url\":\"https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/\",\"name\":\"React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50 - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\"},\"datePublished\":\"2019-05-03T03:51:15+00:00\",\"dateModified\":\"2019-05-03T03:51:31+00:00\",\"author\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.369usa.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\",\"url\":\"https:\/\/www.369usa.com\/blog\/\",\"name\":\"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.369usa.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\",\"name\":\"darkersss\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g\",\"caption\":\"darkersss\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50 - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.369usa.com\/blog\/react-redux-counter\u4ee5\u53careducer-mapstatetoprops-\u548c-mapdispatchtoprops\u4f8b\u5b50\/","og_locale":"en_US","og_type":"article","og_title":"React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50 - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","og_description":"Source code Github location: https:\/\/github.com\/howareyoucolin\/React-Redux-Counter Reducer const initialState = { count: 10 }; function rootReducer(state = initialState, action) { if(action.type == 'INCREASE_COUNT'){ return { ...state, count: (state.count + 1) &gt; 20 ? 20 : state.count + 1 } } else if(action.type == 'DECREASE_COUNT'){ return { ...state, count: (state.count - 1) &lt; 0 ? 0 : &hellip; Continue reading \"React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50\"","og_url":"https:\/\/www.369usa.com\/blog\/react-redux-counter\u4ee5\u53careducer-mapstatetoprops-\u548c-mapdispatchtoprops\u4f8b\u5b50\/","og_site_name":"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","article_published_time":"2019-05-03T03:51:15+00:00","article_modified_time":"2019-05-03T03:51:31+00:00","author":"darkersss","twitter_card":"summary_large_image","twitter_misc":{"Written by":"darkersss","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/","url":"https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/","name":"React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50 - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","isPartOf":{"@id":"https:\/\/www.369usa.com\/blog\/#website"},"datePublished":"2019-05-03T03:51:15+00:00","dateModified":"2019-05-03T03:51:31+00:00","author":{"@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6"},"breadcrumb":{"@id":"https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.369usa.com\/blog\/react-redux-counter%e4%bb%a5%e5%8f%8areducer-mapstatetoprops-%e5%92%8c-mapdispatchtoprops%e4%be%8b%e5%ad%90\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.369usa.com\/blog\/"},{"@type":"ListItem","position":2,"name":"React-Redux Counter\u4ee5\u53caReducer, mapStateToProps \u548c mapDispatchToProps\u4f8b\u5b50"}]},{"@type":"WebSite","@id":"https:\/\/www.369usa.com\/blog\/#website","url":"https:\/\/www.369usa.com\/blog\/","name":"369\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.369usa.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6","name":"darkersss","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2c538d84b753eed6f6f714becf9b3955?s=96&d=mm&r=g","caption":"darkersss"}}]}},"_links":{"self":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/82"}],"collection":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/comments?post=82"}],"version-history":[{"count":1,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions"}],"predecessor-version":[{"id":83,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/82\/revisions\/83"}],"wp:attachment":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/media?parent=82"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/categories?post=82"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/tags?post=82"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}