{"id":2874,"date":"2020-04-19T19:55:32","date_gmt":"2020-04-19T11:55:32","guid":{"rendered":"http:\/\/www.yuguoxy.com\/geekhome\/?p=2874"},"modified":"2026-08-18T12:35:29","modified_gmt":"2026-08-18T04:35:29","slug":"%e3%80%8c%e6%8e%a8%e8%8d%90%e3%80%8dreact-%e9%80%9a%e4%bf%a1%e4%b9%8b-redux%ef%bc%8c%e7%9c%8b%e8%bf%99%e4%b8%80%e7%af%87%e5%b0%b1%e8%83%bd%e6%8e%8c%e6%8f%a1","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=2874","title":{"rendered":"\u300c\u63a8\u8350\u300dReact \u901a\u4fe1\u4e4b Redux\uff0c\u770b\u8fd9\u4e00\u7bc7\u5c31\u80fd\u638c\u63e1"},"content":{"rendered":"<section id=\"wenyan\" style=\"font-family: system-ui, 'Apple Color Emoji', 'Segoe UI', 'Segoe UI Symbol', 'Noto Sans', Roboto, sans-serif; line-height: 1.75; font-size: 16px; color: #000000; caret-color: #000000;\" data-provider=\"WenYan\">\n<p style=\"margin: 1em 0px;\">React \u7ec4\u4ef6\u5e38\u5728 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">componentDidMount<\/code> \u91cc\u521d\u59cb\u5316 Model \u5e76\u76d1\u542c change\uff0cModel \u53d8\u4e86\u518d <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">setState<\/code> \u91cd\u7ed8\uff1b<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">componentWillUnmount<\/code> \u65f6\u53d6\u6d88\u76d1\u542c\u5e76\u9500\u6bc1 Model\u3002\u72b6\u6001\u4e00\u590d\u6742\uff0c\u5355\u5411\u6570\u636e\u6d41\u4f1a\u8d8a\u6765\u8d8a\u7ed5\u3002<strong style=\"color: #4870ac;\">Redux<\/strong> \u5c31\u662f\u4e3a\u4e86\u628a state \u6536\u62e2\u5230\u5355\u4e00\u4ed3\u5e93\u91cc\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/redux1.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2876\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/redux1.png\" alt=\"redux1\" width=\"620\" height=\"469\"><\/a><\/p>\n<p style=\"margin: 1em 0px;\">\u5b58\u5728\u5373\u5408\u7406\u3002\u4e0b\u56fe\u5de6\u4fa7\u662f React \u5355\u5411\u6d41\u52a8\u5728\u590d\u6742\u72b6\u6001\u9762\u524d\u7684\u9ebb\u70e6\uff1b\u53f3\u4fa7\u662f Redux \u7ef4\u62a4\u7684\u5355\u4e00\u72b6\u6001\u6811\u3002\u4e09\u5927\u7279\u6027\uff1a<strong style=\"color: #4870ac;\">\u552f\u4e00\u6570\u636e\u6e90<\/strong>\u3001<strong style=\"color: #4870ac;\">\u4fdd\u6301\u53ea\u8bfb<\/strong>\u3001<strong style=\"color: #4870ac;\">\u53ea\u80fd\u7528\u7eaf\u51fd\u6570\u6539\u6570\u636e<\/strong>\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/redux20200419194631.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2877\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/redux20200419194631.png\" alt=\"redux20200419194631\" width=\"560\" height=\"325\"><\/a><\/p>\n<table style=\"display: table; text-align: left; overflow: auto; border-collapse: collapse; margin: 1.4em auto; max-width: 100%; table-layout: fixed; overflow-wrap: break-word; word-break: break-all;\" width=\"800\">\n<thead>\n<tr>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u6982\u5ff5<\/th>\n<th style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top; text-align: center; font-weight: bold; color: #4870ac; background: #f7f7f7;\">\u89d2\u8272<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Action<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u628a\u6570\u636e\u4ece\u5e94\u7528\u4f20\u5230 store \u7684\u6709\u6548\u8f7d\u8377<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Store<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u4fdd\u5b58\u6570\u636e\u7684\u5730\u65b9<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Dispatcher<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">view \u53d1\u51fa Action \u7684\u552f\u4e00\u529e\u6cd5<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">Reducer<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">\u6839\u636e Action \u7b97\u51fa\u65b0 state\uff0cview \u624d\u4f1a\u53d8<\/td>\n<\/tr>\n<tr>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">State<\/td>\n<td style=\"border: 1px solid #d9dfe4; padding: 9px 12px; font-size: 0.75em; line-height: 22px; vertical-align: top;\">store \u91cc\u5b58\u7684\u6570\u636e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e00\u3001\u7528\u501f\u4e66\u628a\u6d41\u7a0b\u4e32\u8d77\u6765<\/h2>\n<p style=\"margin: 1em 0px;\">\u56de\u60f3\u53bb\u56fe\u4e66\u9986\u501f\u4e66\uff0c\u5bf9\u7167\u4e0a\u56fe\u53f3\u4fa7\u7684\u6811\uff1a<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><strong style=\"color: #4870ac;\">\u4f60<\/strong>\u53bb\u56fe\u4e66\u9986\uff1b<\/section>\n<\/li>\n<li>\n<section><strong style=\"color: #4870ac;\">\u8be2\u95ee<\/strong>\u7ba1\u7406\u5458\u6709\u6ca1\u6709\u300a\u897f\u90e8\u4e16\u754c iii\u300b\uff1b<\/section>\n<\/li>\n<li>\n<section>\u7ba1\u7406\u5458<strong style=\"color: #4870ac;\">\u67e5\u9605<\/strong>\u5e93\u5b58\uff1b<\/section>\n<\/li>\n<li>\n<section><strong style=\"color: #4870ac;\">\u53cd\u9988<\/strong>\u7ed9\u4f60\uff1a\u81ea\u5df1\u7f51\u4e0a\u627e\u8d44\u6e90\u53bb\uff1b<\/section>\n<\/li>\n<li>\n<section>\u4f60\u53bb\u7f51\u4e0a\u627e\u8d44\u6e90\u3002<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\">\u8fd9\u5c31\u662f Redux \u5168\u6d41\u7a0b\u3002<strong style=\"color: #4870ac;\">\u8be2\u95ee + \u67e5\u9605 = \u53cd\u9988<\/strong>\uff0c\u4e5f\u5c31\u662f <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">action + state = new state<\/code>\u3002\u6709\u679c\u5fc5\u6709\u56e0\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/20200419194702.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2878\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/20200419194702.png\" alt=\"20200419194702\" width=\"457\" height=\"293\"><\/a><\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/20200419194725.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2879\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/20200419194725.png\" alt=\"20200419194725\" width=\"429\" height=\"278\"><\/a><\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/20200419194743.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2880\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/20200419194743.png\" alt=\"20200419194743\" width=\"638\" height=\"354\"><\/a><\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e8c\u3001UI \u7ec4\u4ef6\u548c\u5bb9\u5668\u7ec4\u4ef6<\/h2>\n<p style=\"margin: 1em 0px;\">React-Redux \u628a\u7ec4\u4ef6\u5206\u6210 <strong style=\"color: #4870ac;\">UI \u7ec4\u4ef6<\/strong>\uff08presentational\uff09\u548c <strong style=\"color: #4870ac;\">\u5bb9\u5668\u7ec4\u4ef6<\/strong>\uff08container\uff09\u3002<\/p>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">UI \u7ec4\u4ef6\u8d1f\u8d23\u5448\u73b0\uff0c\u5bb9\u5668\u7ec4\u4ef6\u8d1f\u8d23\u6570\u636e\u548c\u903b\u8f91\u3002<\/strong><\/p>\n<\/blockquote>\n<p style=\"margin: 1em 0px;\">\u4e24\u8005\u90fd\u6709\u600e\u4e48\u529e\uff1f\u62c6\u5f00\uff1a\u5916\u9762\u5bb9\u5668\u3001\u91cc\u9762 UI\u3002\u5bb9\u5668\u5bf9\u5916\u901a\u4fe1\uff0c\u628a\u6570\u636e\u4f20\u7ed9 UI \u6e32\u67d3\u3002\u89c4\u5b9a\u662f\uff1aUI \u7531\u7528\u6237\u63d0\u4f9b\uff0c\u5bb9\u5668\u7531 React-Redux \u81ea\u52a8\u751f\u6210\u2014\u2014\u89c6\u89c9\u5c42\u5f52\u4f60\uff0c\u72b6\u6001\u7ba1\u7406\u4ea4\u7ed9\u5b83\u3002\u4ee5\u4e0a\u6458\u81ea\u962e\u4e00\u5cf0\u7f51\u7edc\u65e5\u5fd7\uff0c\u5efa\u8bae\u521d\u5b66\u8005\u591a\u8bfb\u51e0\u904d\u3002<\/p>\n<ul style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem; list-style-type: disc;\">\n<li>\n<section><strong style=\"color: #4870ac;\">Action\uff1a<\/strong> \u628a\u6570\u636e\u4ece\u5e94\u7528\u4f20\u5230 store \u7684\u6709\u6548\u8f7d\u8377\uff1b<\/section>\n<\/li>\n<li>\n<section><strong style=\"color: #4870ac;\">Store<\/strong>: \u4fdd\u5b58\u6570\u636e\u7684\u5730\u65b9\uff1b<\/section>\n<\/li>\n<li>\n<section><strong style=\"color: #4870ac;\">Dispatcher<\/strong>: view \u53d1\u51fa Action \u7684\u552f\u4e00\u529e\u6cd5\uff1b<\/section>\n<\/li>\n<li>\n<section><strong style=\"color: #4870ac;\">Reducer\uff1a<\/strong> Store \u6536\u5230 Action \u540e\u5fc5\u987b\u7ed9\u51fa\u65b0 state\u3002<strong style=\"color: #4870ac;\">Reducer \u5373\u6839\u636e Action \u8ba1\u7b97 state\uff1b<\/strong><\/section>\n<\/li>\n<li>\n<section><strong style=\"color: #4870ac;\">State<\/strong>: store \u91cc\u9762\u5b58\u50a8\u7684\u6570\u636e\u3002<\/section>\n<\/li>\n<\/ul>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/QQ\u255c\u256a\u2550\u255d20200419195109.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2884\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/QQ\u255c\u256a\u2550\u255d20200419195109.png\" alt=\"QQ\u255c\u256a\u2550\u255d20200419195109\" width=\"575\" height=\"438\"><\/a><\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/QQ\u255c\u256a\u2550\u255d20200419194929.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2881\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/QQ\u255c\u256a\u2550\u255d20200419194929.png\" alt=\"QQ\u255c\u256a\u2550\u255d20200419194929\" width=\"633\" height=\"519\"><\/a><\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e09\u3001\u52a0\u6570\u793a\u4f8b\uff1a\u8d70\u5b8c\u4e00\u904d\u6d41\u7a0b<\/h2>\n<p style=\"margin: 1em 0px;\">\u4e00\u4e2a\u7b80\u5355\u52a0\u6570\uff0c\u8986\u76d6 Redux \u5404\u6982\u5ff5\u3001UI \u4e0e\u5bb9\u5668\u7684\u5173\u7cfb\uff0c\u4ee5\u53ca Provider\u3002<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">import React, { Component } from \"react\";\nimport PropTypes from \"prop-types\";\nimport ReactDOM from \"react-dom\";\nimport { createStore } from \"redux\";\nimport { Provider, connect } from \"react-redux\";\n\n\/\/ \u751f\u6210\u7ec4\u4ef6\nclass Counter extends Component {\n  render() {\n    const { value, onIncreaseClick } = this.props;\n    return (\n      &lt;div&gt;\n        &lt;span&gt;{value}&lt;\/span&gt;\n        &lt;button onClick={onIncreaseClick}&gt;Increase&lt;\/button&gt;\n      &lt;\/div&gt;\n    );\n  }\n}\n\nCounter.propTypes = {\n  value: PropTypes.number.isRequired,\n  onIncreaseClick: PropTypes.func.isRequired\n};\n\n\/\/ \u5b9a\u4e49action\nconst increaseAction = { type: \"increase\" };\n\n\/**\n * Reducer\u51fd\u6570\n * @param {*} state \u9ed8\u8ba4\u503c\n * @param {*} action \u6839\u636eaction\u6765\u8ba1\u7b97state.\n *\/\nfunction counter(state = { count: 0 }, action) {\n  const count = state.count;\n  switch (action.type) {\n    case \"increase\":\n      return { count: count + 1 };\n    default:\n      return state;\n  }\n}\n\n\/**\n * \u521b\u5efastore -&gt; \u9700\u8981reducer -&gt; \u9ed8\u8ba4\u503c+action\n *\/\nconst store = createStore(counter);\n\n\/**\n * \u5efa\u7acb\u4e00\u4e2a\u4ece\uff08\u5916\u90e8\u7684\uff09state\u5bf9\u8c61\u5230\uff08UI \u7ec4\u4ef6\u7684\uff09props\u5bf9\u8c61\u7684\u6620\u5c04\u5173\u7cfb\u3002\n * @param {*} state\n *\/\nfunction mapStateToProps(state) {\n  return {\n    value: state.count\n  };\n}\n\n\/**\n * \u5efa\u7acb UI \u7ec4\u4ef6\u7684\u53c2\u6570\u5230store.dispatch\u65b9\u6cd5\u7684\u6620\u5c04\n * \u5b9a\u4e49\u4e86\u54ea\u4e9b\u7528\u6237\u7684\u64cd\u4f5c\u5e94\u8be5\u5f53\u4f5c Action\uff0c\u4f20\u7ed9 Store\u3002\n * \u5b83\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u5bf9\u8c61\u3002\n *\n * 1. \u51fd\u6570\uff1a\n *      - \u53c2\u6570\uff1a\n *        \u53ef\u4ee5\u62ff\u5230dispatch\u548cownProps\uff08\u5bb9\u5668\u7ec4\u4ef6\u7684props\u5bf9\u8c61\uff09\u4e24\u4e2a\u53c2\u6570\uff1b\n *      - \u8fd4\u56de\u503c\n *        \u4e00\u4e2a\u5bf9\u8c61\uff0c\u6bcf\u4e2a\u952e\u503c\u5bf9\u90fd\u662f\u4e00\u4e2a\u6620\u5c04\uff0c[\u952e\u540d\u5bf9\u5e94\u51fd\u6570\u540d]\n *         \u5b9a\u4e49\u4e86 UI \u7ec4\u4ef6\u7684\u53c2\u6570\u600e\u6837\u53d1\u51fa Action\u3002\n * 2. \u5bf9\u8c61\n *      \u5b83\u7684\u6bcf\u4e2a\u952e\u540d\u4e5f\u662f\u5bf9\u5e94 UI \u7ec4\u4ef6\u7684\u540c\u540d\u53c2\u6570\uff0c\u952e\u503c\u5e94\u8be5\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\n *      \u4f1a\u88ab\u5f53\u4f5c Action creator \uff0c\u8fd4\u56de\u7684 Action \u4f1a\u7531 Redux \u81ea\u52a8\u53d1\u51fa\u3002\n * @param {*} dispatch\n *\/\nfunction mapDispatchToProps(dispatch) {\n  return {\n    \/\/ \u540c\u540d\u51fd\u6570 onIncreaseClick\uff0c dispatch \u4e00\u4e2aaction\n    onIncreaseClick: () =&gt; dispatch(increaseAction)\n  };\n}\n\n\/**\n * connect \u81ea\u52a8\u751f\u6210\u5bb9\u5668\u7ec4\u4ef6\uff0c\u5c06UI\u7ec4\u4ef6[Counter]\u4e0e[\u5bb9\u5668\u7ec4\u4ef6]\u5373\u5173\u8054\u8d77\u6765\u3002\n * Counter: UI \u7ec4\u4ef6\n * mapStateToProps\uff1a \u8f93\u5165\u903b\u8f91\uff1a\u5916\u90e8\u7684\u6570\u636e\uff08\u5373state\u5bf9\u8c61\uff09\u5982\u4f55\u8f6c\u6362\u4e3a UI \u7ec4\u4ef6\u7684\u53c2\u6570\n * mapDispatchToProps\uff1a \u7528\u6237\u53d1\u51fa\u7684\u52a8\u4f5c\u5982\u4f55\u53d8\u4e3a Action \u5bf9\u8c61\uff0c\u4ece UI \u7ec4\u4ef6\u4f20\u51fa\u53bb\u3002\n *\/\nconst App = connect(\n  mapStateToProps,\n  mapDispatchToProps\n)(Counter);\n\n\/\/ React-Redux \u63d0\u4f9bProvider\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u8ba9\u5bb9\u5668\u7ec4\u4ef6\u62ff\u5230state\u3002\n\/\/ Provider\u5728\u6839\u7ec4\u4ef6\u5916\u9762\u5305\u4e86\u4e00\u5c42\uff0c\u8fd9\u6837\u4e00\u6765\uff0cApp\u7684\u6240\u6709\u5b50\u7ec4\u4ef6\u5c31\u9ed8\u8ba4\u90fd\u53ef\u4ee5\u62ff\u5230state\u4e86\n\/\/ \u539f\u7406\uff1a React\u4e2d\u7684context\u5c5e\u6027\uff0c\nReactDOM.render(\n  &lt;Provider store={store}&gt;\n    &lt;App \/&gt;\n  &lt;\/Provider&gt;,\n  document.getElementById(\"root\")\n);<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u7ed3\u5408\u4e0b\u56fe\u7406\u89e3\uff1a<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/QQ\u255c\u256a\u2550\u255d20200419194948.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2882\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/QQ\u255c\u256a\u2550\u255d20200419194948.png\" alt=\"QQ\u255c\u256a\u2550\u255d20200419194948\" width=\"624\" height=\"601\"><\/a><\/p>\n<p style=\"margin: 1em 0px;\">\u56fe\u7247\u6765\u6e90\uff1ahttps:\/\/juejin.im\/post\/5acdbe8f51882548fe4a7af1<\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u56db\u3001Provider \u7a76\u7adf\u505a\u4e86\u4ec0\u4e48<\/h2>\n<p style=\"margin: 1em 0px;\">\u6700\u5916\u5c42\u7528 react-redux \u7684 Provider\u3002\u5b83\u672c\u8eab\u5c31\u662f React \u7ec4\u4ef6\uff0c\u539f\u7406\u662f <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">context<\/code>\uff1a\u6700\u5916\u5c42\u5b9e\u73b0 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">getChildContext<\/code> \u8fd4\u56de\u5bf9\u8c61\uff0c\u5185\u90e8\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">this.context<\/code> \u62ff\u3002\u7b80\u5316\u5b9e\u73b0\uff1a<\/p>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">class Provider extends React.Component {\n  getChildContext() {\n    return { store: this.props.store }    \n  }\n\n  render() {\n    return this.props.children\n  }\n}\n\n\/\/ usage\nconst store = createStore();\nReactDOM.render(\n  &lt;Provider store={store}&gt;\n    &lt;App \/&gt;\n  &lt;\/Provider&gt;\n, document.querySelector('#root'));<\/code><\/pre>\n<p style=\"margin: 1em 0px;\">\u6574\u9875\u903b\u8f91\uff1a<\/p>\n<ol style=\"padding-left: 1rem; margin-left: 1rem; font-size: 0.9rem;\">\n<li>\n<section>\u521b\u5efa Store\uff1a<code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">createStore()<\/code>\uff0c\u63a5\u53d7 reducers \u548c\u9ed8\u8ba4\u503c\uff1b<\/section>\n<\/li>\n<li>\n<section>createStore \u5bf9\u6bcf\u4e2a reducer dispatch \u4e00\u4e2a <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">action.type=@@redux\/INIT<\/code> \u7684 action\uff0c\u5f97\u5230\u9ed8\u8ba4 state\uff1b<\/section>\n<\/li>\n<li>\n<section><code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">render()<\/code> \u65f6\uff0c\u5b50\u7ec4\u4ef6\u7528 <code style=\"font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; color: #4870ac; font-size: 0.9em; font-weight: normal; overflow-wrap: break-word; padding: 2px 4px; border-radius: 3px; margin: 2px; background-color: #f6f8fa; word-break: break-all;\">this.context.store.getState()<\/code> \u62ff state\uff0c\u52a0\u4e0a ownProps \u4ea4\u7ed9 mapStateToProps\uff0c\u6784\u6210 props\uff1b<\/section>\n<\/li>\n<li>\n<section>UI \u89e6\u53d1\u64cd\u4f5c\uff0cmapDispatchToProps \u53d1\u51fa\u5bf9\u5e94 action\uff1b<\/section>\n<\/li>\n<li>\n<section>store \u6536\u5230\u540e reduce \u51fa\u65b0 state\uff0c\u518d\u8c03\u5404\u5b50\u7ec4\u4ef6\u7684\u6620\u5c04\u751f\u6210 props\uff08\u539f\u6587\u5199 mapDispatchToProps\uff0c\u5b9e\u9645\u66f4\u65b0\u89c6\u56fe\u9760\u7684\u662f\u8ba2\u9605\u540e\u7684 mapStateToProps\uff09\uff1b<\/section>\n<\/li>\n<li>\n<section>\u6700\u540e Provider \u9a71\u52a8\u9700\u8981\u66f4\u65b0\u7684\u7ec4\u4ef6\u91cd\u6e32\u67d3\u3002<\/section>\n<\/li>\n<\/ol>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u4e94\u3001TodoList\uff1a\u81ea\u5df1\u8d70\u4e00\u904d<\/h2>\n<p style=\"margin: 1em 0px;\">react-redux \u7684 TodoList demo \u5efa\u8bae\u4ece\u5934\u5b9e\u73b0\u4e00\u904d\u3002\u4ee3\u7801\u867d\u591a\uff0c\u5bf9\u7167\u4e0a\u9762\u7684\u7406\u89e3\u5c31\u80fd\u8fc7\uff1b\u4e0d\u8981\u773c\u9ad8\u624b\u4f4e\u3002<\/p>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/QQ\u255c\u256a\u2550\u255d20200419195003.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-2883\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2020\/04\/QQ\u255c\u256a\u2550\u255d20200419195003.png\" alt=\"QQ\u255c\u256a\u2550\u255d20200419195003\" width=\"623\" height=\"495\"><\/a><\/p>\n<h2 style=\"padding: 1px 12.5px; color: #ffffff; margin: 1.2em 0px 1em; border-radius: 4px; display: inline-block; background-color: #4870ac; font-size: 1.3em;\">\u516d\u3001\u53c2\u8003<\/h2>\n<pre style=\"overflow: auto hidden; word-break: normal; white-space: pre; tab-size: 2; border-radius: 5px; line-height: 2; margin: 1em 0.5em; padding: 0.5em; box-shadow: rgba(0, 0, 0, 0.55) 0px 1px 5px; font-size: 12px; background: #002b36; max-width: 100%; overflow-wrap: normal;\"><code class=\"hljs\" style=\"word-break: normal; white-space: nowrap; tab-size: 2; font-size: 12px; font-family: Menlo, Monaco, Consolas, 'Liberation Mono', 'Roboto Mono', 'Courier New', 'Microsoft YaHei', monospace; display: block; overflow-x: auto; margin: 0.5em; padding: 3px 5px; color: #93a1a1; background: #002b36; overflow-wrap: normal;\">\u4e00\u5e45\u56fe\u660e\u767dReact-Redux\u7684\u539f\u7406\n\u89e3\u6790\uff1a\u8ba9\u4f60\u5f04\u61c2redux\u539f\u7406\nhttps:\/\/www.cnblogs.com\/nayek\/p\/12369418.html\nhttps:\/\/www.processon.com\/view\/5db55958e4b0e433944fdc87\nhttps:\/\/www.ruanyifeng.com\/blog\/2016\/09\/redux_tutorial_part_three_react-redux.html<\/code><\/pre>\n<p style=\"margin: 1em 0px;\"><a href=\"https:\/\/www.toutiao.com\/a6808016650248389131\/?tt_from=mobile_qq&amp;utm_campaign=client_share&amp;timestamp=1585123799&amp;app=news_article&amp;utm_source=mobile_qq&amp;utm_medium=toutiao_android&amp;req_id=202003251609590100140411593203CC76&amp;group_id=6808016650248389131\">\u539f\u6587<\/a><\/p>\n<blockquote style=\"display: block; font-size: 0.9em; border-left: 3px solid #4870ac; padding: 0.5em 1em; margin: 0px; background: #f6f8fa; color: #40464f;\">\n<p style=\"margin: 1em 0px;\"><strong style=\"color: #4870ac;\">\u4e00\u53e5\u8bdd\u603b\u7ed3\uff1aRedux \u7528\u552f\u4e00\u53ea\u8bfb store \u548c\u7eaf\u51fd\u6570 reducer \u6d88\u5316 action\uff1bReact-Redux \u518d\u7528\u5bb9\u5668\u7ec4\u4ef6\u548c Provider \u7684 context \u628a state \u704c\u8fdb UI\u3002<\/strong><\/p>\n<\/blockquote>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>React \u7ec4\u4ef6\u5e38\u5728 componentDidMount \u91cc\u521d\u59cb\u5316 Model \u5e76\u76d1\u542c change\uff0cMode [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[125,105],"tags":[143,137],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2874"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2874"}],"version-history":[{"count":3,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2874\/revisions"}],"predecessor-version":[{"id":3691,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/2874\/revisions\/3691"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}