{"id":70,"date":"2019-04-29T02:41:35","date_gmt":"2019-04-29T02:41:35","guid":{"rendered":"https:\/\/www.369usa.com\/blog\/?p=70"},"modified":"2019-04-29T02:41:46","modified_gmt":"2019-04-29T02:41:46","slug":"redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable","status":"publish","type":"post","link":"https:\/\/www.369usa.com\/blog\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/","title":{"rendered":"Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable)"},"content":{"rendered":"<p>Redux\u5176\u5b9e\u5c31\u8fd9\u6837\u7684\u4e00\u56de\u4e8b\uff0c\u5341\u5206\u7684\u7b80\u5355\u3002<\/p>\n<pre>import { createStore } from \"redux\";\r\n\r\nconst initialState = {\r\n\tcounter: 0\r\n};\r\n\r\n\/\/Reducer\r\nfunction reducer(state = initialState, action) {\r\n\tif (action.type === 'COUNT_UP') {\r\n\t\t\/\/Mutable state(NO GOOD!)\r\n\t\t\/*\r\n\t\tstate.counter = state.counter + action.payload;\r\n\t\treturn state;\r\n\t\t*\/\r\n\t\t\/\/Immutable state\r\n\t\tlet newState = Object.assign({}, state);\r\n\t\tnewState.counter = state.counter + action.payload;\r\n\t\treturn newState;\r\n\t}\r\n\treturn state;\r\n};\r\n\r\n\/\/Action\r\nfunction countUp(payload) {\r\n  return { type: 'COUNT_UP', payload };\r\n}\r\n\r\n\/\/Store\r\nconst store = createStore(reducer);\r\n\r\n\/\/Subscribe method accepts a callback that will fire whenever an action is dispatched.\r\nconst result = [];\r\nstore.subscribe(() =&gt; {\r\n\tresult.push(store.getState());\r\n\tconsole.log(result);\r\n})\r\n\r\nconsole.log(store.getState());\r\n\r\n\/\/Dispatch\r\nstore.dispatch( countUp(1) );\r\nstore.dispatch( countUp(2) );\r\nstore.dispatch( countUp(3) );\r\nstore.dispatch( countUp(4) );\r\nstore.dispatch( countUp(5) );\r\nstore.dispatch( countUp(6) );\r\n<\/pre>\n<p>Output:<\/p>\n<pre>[ ]\r\n\r\n[ { counter: 1 } ]\r\n\r\n[ { counter: 1 }, { counter: 3 } ]\r\n\r\n[ { counter: 1 }, { counter: 3 }, { counter: 6 } ]\r\n\r\n[ { counter: 1 },\r\n  { counter: 3 },\r\n  { counter: 6 },\r\n  { counter: 10 } ]\r\n\r\n[ { counter: 1 },\r\n  { counter: 3 },\r\n  { counter: 6 },\r\n  { counter: 10 },\r\n  { counter: 15 } ]\r\n\r\n[ { counter: 1 },\r\n  { counter: 3 },\r\n  { counter: 6 },\r\n  { counter: 10 },\r\n  { counter: 15 },\r\n  { counter: 21 } ]\r\n\r\n<\/pre>\n<p>See?\u5c31\u8fd9\u4e48\u7684\u7b80\u5355\u3002<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<br \/>\n<a href=\"https:\/\/www.valentinog.com\/blog\/redux\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.valentinog.com\/blog\/redux\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redux\u5176\u5b9e\u5c31\u8fd9\u6837\u7684\u4e00\u56de\u4e8b\uff0c\u5341\u5206\u7684\u7b80\u5355\u3002 import { createStore } from &#8220;redux&#8221;; const initialState = { counter: 0 }; \/\/Reducer function reducer(state = initialState, action) { if (action.type === &#8216;COUNT_UP&#8217;) { \/\/Mutable state(NO GOOD!) \/* state.counter = state.counter + action.payload; return state; *\/ \/\/Immutable state let newState = Object.assign({}, state); newState.counter = state.counter + action.payload; return newState; } return state; &hellip; <a href=\"https:\/\/www.369usa.com\/blog\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable)&#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":[4],"tags":[],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-4"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable) - \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\/redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801immutable\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable) - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"Redux\u5176\u5b9e\u5c31\u8fd9\u6837\u7684\u4e00\u56de\u4e8b\uff0c\u5341\u5206\u7684\u7b80\u5355\u3002 import { createStore } from &quot;redux&quot;; const initialState = { counter: 0 }; \/\/Reducer function reducer(state = initialState, action) { if (action.type === &#039;COUNT_UP&#039;) { \/\/Mutable state(NO GOOD!) \/* state.counter = state.counter + action.payload; return state; *\/ \/\/Immutable state let newState = Object.assign({}, state); newState.counter = state.counter + action.payload; return newState; } return state; &hellip; Continue reading &quot;Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable)&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.369usa.com\/blog\/redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801immutable\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-29T02:41:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-29T02:41:46+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\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/\",\"url\":\"https:\/\/www.369usa.com\/blog\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/\",\"name\":\"Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable) - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#website\"},\"datePublished\":\"2019-04-29T02:41:35+00:00\",\"dateModified\":\"2019-04-29T02:41:46+00:00\",\"author\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.369usa.com\/blog\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.369usa.com\/blog\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.369usa.com\/blog\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.369usa.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable)\"}]},{\"@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":"Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable) - \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\/redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801immutable\/","og_locale":"en_US","og_type":"article","og_title":"Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable) - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","og_description":"Redux\u5176\u5b9e\u5c31\u8fd9\u6837\u7684\u4e00\u56de\u4e8b\uff0c\u5341\u5206\u7684\u7b80\u5355\u3002 import { createStore } from \"redux\"; const initialState = { counter: 0 }; \/\/Reducer function reducer(state = initialState, action) { if (action.type === 'COUNT_UP') { \/\/Mutable state(NO GOOD!) \/* state.counter = state.counter + action.payload; return state; *\/ \/\/Immutable state let newState = Object.assign({}, state); newState.counter = state.counter + action.payload; return newState; } return state; &hellip; Continue reading \"Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable)\"","og_url":"https:\/\/www.369usa.com\/blog\/redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801immutable\/","og_site_name":"\u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","article_published_time":"2019-04-29T02:41:35+00:00","article_modified_time":"2019-04-29T02:41:46+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\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/","url":"https:\/\/www.369usa.com\/blog\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/","name":"Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable) - \u7ebd\u7ea6\u7f51\u7ad9\u8bbe\u8ba1\u535a\u5ba2","isPartOf":{"@id":"https:\/\/www.369usa.com\/blog\/#website"},"datePublished":"2019-04-29T02:41:35+00:00","dateModified":"2019-04-29T02:41:46+00:00","author":{"@id":"https:\/\/www.369usa.com\/blog\/#\/schema\/person\/1c260c42af4aadda7f89a0081f6591b6"},"breadcrumb":{"@id":"https:\/\/www.369usa.com\/blog\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.369usa.com\/blog\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.369usa.com\/blog\/redux%e7%9a%84%e6%9c%80%e5%9f%ba%e6%9c%ac%e6%a0%b7%e6%9c%ac%e4%bb%a3%e7%a0%81immutable\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.369usa.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Redux\u7684\u6700\u57fa\u672c\u6837\u672c\u4ee3\u7801(Immutable)"}]},{"@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\/70"}],"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=70"}],"version-history":[{"count":1,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"predecessor-version":[{"id":71,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/posts\/70\/revisions\/71"}],"wp:attachment":[{"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.369usa.com\/blog\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}