{"id":201568,"date":"2025-05-10T08:18:35","date_gmt":"2025-05-10T00:18:35","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201568\/"},"modified":"2025-05-10T08:18:35","modified_gmt":"2025-05-10T00:18:35","slug":"react-%e7%bb%84%e4%bb%b6%e7%9a%84%e5%b8%b8%e7%94%a8%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%e5%87%bd%e6%95%b0%e6%b1%87%e6%80%bb","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201568\/","title":{"rendered":"React\u00a0\u7ec4\u4ef6\u7684\u5e38\u7528\u751f\u547d\u5468\u671f\u51fd\u6570\u6c47\u603b"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>React&nbsp;\u7ec4\u4ef6\u7684\u5e38\u7528\u751f\u547d\u5468\u671f\u51fd\u6570\u6c47\u603b<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aReact&nbsp;\u7ec4\u4ef6\u7684\u5e38\u7528\u751f\u547d\u5468\u671f\u51fd\u6570\u6c47\u603b\u300b\uff0c\u4e3b\u8981\u5305\u62ec\u51fd\u6570\u3001React\u7ec4\u4ef6\u3001\u751f\u547d\u3001\u5468\u671f\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<h2>1. \u6982\u8ff0<\/h2>\n<ul>\n<li><strong>\u610f\u4e49\uff1a<\/strong>\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f\u6709\u52a9\u4e8e\u7406\u89e3\u7ec4\u4ef6\u7684\u8fd0\u884c\u65b9\u5f0f\u3001\u5b8c\u6210\u66f4\u590d\u6742\u7684\u7ec4\u4ef6\u529f\u80fd\u3001\u5206\u6790\u7ec4\u4ef6\u9519\u8bef\u539f\u56e0\u7b49\u3002<\/li>\n<li><strong>\u7ec4\u4ef6\u7684\u751f\u547d\u5468\u671f\uff1a<\/strong>\u7ec4\u4ef6\u4ece\u88ab\u521b\u5efa\u5230\u6302\u8f7d\u5230\u9875\u9762\u4e2d\u8fd0\u884c\uff0c\u518d\u5230\u7ec4\u4ef6\u4e0d\u7528\u65f6\u5378\u8f7d\u7684\u8fc7\u7a0b\u3002<\/li>\n<li>\u751f\u547d\u5468\u671f\u7684\u6bcf\u4e2a\u9636\u6bb5\u603b\u662f\u4f34\u968f\u7740\u4e00\u4e9b\u65b9\u6cd5\u8c03\u7528\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5c31\u662f\u751f\u547d\u5468\u671f\u7684\u94a9\u5b50\u51fd\u6570\u3002<\/li>\n<li><strong>\u94a9\u5b50\u51fd\u6570\u7684\u4f5c\u7528\uff1a<\/strong>\u4e3a\u5f00\u53d1\u4eba\u5458\u5728\u4e0d\u540c\u9636\u6bb5\u64cd\u4f5c\u7ec4\u4ef6\u63d0\u4f9b\u4e86\u65f6\u673a\u3002<\/li>\n<li>\u53ea\u6709\u7c7b\u7ec4\u4ef6\u624d\u6709\u751f\u547d\u5468\u671f\u3002<\/li>\n<\/ul>\n<h2>2. \u751f\u547d\u5468\u671f\u7684\u4e09\u4e2a\u9636\u6bb5<\/h2>\n<ul>\n<li>\u6bcf\u4e2a\u9636\u6bb5\u7684\u6267\u884c\u65f6\u673a<\/li>\n<li>\u6bcf\u4e2a\u9636\u6bb5\u94a9\u5b50\u51fd\u6570\u7684\u6267\u884c\u987a\u5e8f<\/li>\n<li>\u6bcf\u4e2a\u9636\u6bb5\u94a9\u5b50\u51fd\u6570\u7684\u4f5c\u7528<\/li>\n<\/ul>\n<h3>2.1. \u521b\u5efa\u65f6\uff08\u6302\u8f7d\u9636\u6bb5\uff09<\/h3>\n<ul>\n<li><strong>\u6267\u884c\u65f6\u673a\uff1a<\/strong>\u7ec4\u4ef6\u521b\u5efa\u65f6\uff08\u9875\u9762\u52a0\u8f7d\u65f6\uff09<\/li>\n<li><strong>\u6267\u884c\u987a\u5e8f\uff1ac<\/strong>onstructor() -&gt; render() -&gt; componentDidMount()<\/li>\n<li><strong>\u94a9\u5b50\u51fd\u6570\u7684\u4f5c\u7528\uff1a<\/strong><\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<th>\u94a9\u5b50\u51fd\u6570<\/th>\n<th>\u89e6\u53d1\u65f6\u673a<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<tr>\n<td>constructor<\/td>\n<td>\u521b\u5efa\u7ec4\u4ef6\u65f6\uff0c\u6700\u5148\u6267\u884c<\/td>\n<td>1.\u521d\u59cb\u5316state 2.\u4e3a\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\u7ed1\u5b9a this<\/td>\n<\/tr>\n<tr>\n<td>render<\/td>\n<td>\u6bcf\u6b21\u7ec4\u4ef6\u6e32\u67d3\u90fd\u4f1a\u89e6\u53d1<\/td>\n<td>\u6e32\u67d3 UI \uff08\u6ce8\u610f\uff1a\u4e0d\u80fd\u8c03\u7528setState()\uff09<\/td>\n<\/tr>\n<tr>\n<td>componentDidMount<\/td>\n<td>\u7ec4\u4ef6\u6302\u8f7d\uff08\u5b8c\u6210 DOM \u6e32\u67d3\uff09\u540e<\/td>\n<td>1.\u53d1\u9001\u7f51\u7edc\u8bf7\u6c42 2.DOM \u64cd\u4f5c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre>\/\/ \u5bfc\u5165ract\nimport React from 'react'\nimport ReactDOM from 'react-dom'\n\nclass App extends React.Component {\n  constructor(props) {\n    super(props)\n\n    \/\/ 1.\u521d\u59cb\u5316state\n    this.state = {\n      count: 0\n    }\n\n    \/\/ 2.\u89e3\u51b3\u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8fthis\u6307\u5411\u95ee\u9898\n    this.handleClick = this.handleClick.bind(this)\n\n    console.warn('\u751f\u547d\u5468\u671f\u94a9\u5b50\u51fd\u6570\uff1aconstructor')\n  }\n  componentDidMount() {\n    \/\/ 1.\u53d1\u9001ajax\u8bf7\u6c42\uff0c\u83b7\u53d6\u8fdc\u7a0b\u6570\u636e\n    \/\/ axios.get('http:\/\/api....')\n\n    \/\/ 2.\u8fdb\u884cDOM\u64cd\u4f5c\n    const title = document.getElementById('title')\n    console.log(title)\n\n    console.warn('\u751f\u547d\u5468\u671f\u94a9\u5b50\u51fd\u6570\uff1acomponentDidMount')\n  }\n\n  \/\/ \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\n  handleClick() {\n    this.setState({\n      count: 1\n    })\n  }\n\n  render() {\n    console.warn('\u751f\u547d\u5468\u671f\u94a9\u5b50\u51fd\u6570\uff1arender')\n\n    \/\/ \u9519\u8bef\u6f14\u793a\uff08\u4e0d\u80fd\u8c03\u7528setState()\uff09\n    \/\/ this.setState({\n    \/\/   count: 2\n    \/\/ })\n\n    return (\n      \n   \n        \n    <h1>\u7edf\u8ba1\u8c46\u8c46\u88ab\u6253\u7684\u6b21\u6570\uff1a{this.state.count}<\/h1>\n        \n    \u6253\u8c46\u8c46\n      \n   \n    )\n  }\n}\nReactDOM.render(\n   , document.getElementById('root'))<\/pre>\n<h3>2.2. \u66f4\u65b0\u65f6\uff08\u66f4\u65b0\u9636\u6bb5\uff09<\/h3>\n<ul>\n<li><strong>\u6267\u884c\u65f6\u673a\uff1a<\/strong>setState()\u3001forceUpdate()\u3001\u7ec4\u4ef6\u63a5\u6536\u5230\u65b0\u7684props\u3002<\/li>\n<li><strong>\u8bf4\u660e\uff1a<\/strong>\u4ee5\u4e0a\u4efb\u610f\u4e00\u79cd\u53d8\u5316\uff0c\u7ec4\u4ef6\u5c31\u4f1a\u91cd\u65b0\u6e32\u67d3\u3002<\/li>\n<li><strong>\u6267\u884c\u987a\u5e8f\uff1a<\/strong>render() -&gt;&nbsp;componentDidUpdate()<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<th>\u94a9\u5b50\u51fd\u6570<\/th>\n<th>\u89e6\u53d1\u65f6\u673a<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<tr>\n<td>render<\/td>\n<td>\u6bcf\u6b21\u7ec4\u4ef6\u6e32\u67d3\u90fd\u4f1a\u89e6\u53d1<\/td>\n<td>\u6e32\u67d3 UI \uff08\u4e0e\u6302\u8f7d\u9636\u6bb5\u662f\u540c\u4e00\u4e2arender\uff09<\/td>\n<\/tr>\n<tr>\n<td>componentDidUpdate<\/td>\n<td>\u7ec4\u4ef6\u66f4\u65b0\uff08\u5b8c\u6210 DOM \u6e32\u67d3\uff09\u540e<\/td>\n<td>1.\u53d1\u9001\u7f51\u7edc\u8bf7\u6c42 2.DOM \u64cd\u4f5c \u6ce8\u610f\uff1a\u5982\u679c\u8981 setState() \u5fc5\u987b\u653e\u5728\u4e00\u4e2aif\u6761\u4ef6\u4e2d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre>\/\/ \u5bfc\u5165ract\nimport React from 'react'\nimport ReactDOM from 'react-dom'\n\n\/\/ \u7236\u7ec4\u4ef6\nclass App extends React.Component {\n  constructor(props) {\n    super(props)\n\n    this.state = {\n      count: 0\n    }\n  }\n\n  \/\/ \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\n  handleClick = () =&gt; {\n    \/\/ \u6267\u884c\u65f6\u673a\uff1asetState()\n    this.setState({\n      count: this.state.count + 1\n    })\n\n    \/\/ \u6267\u884c\u65f6\u673a\uff1a\u5f3a\u5236\u66f4\u65b0\n    \/\/ this.forceUpdate()\n  }\n\n  render() {\n    return (\n      \n   \n    \n        {\/* \u6267\u884c\u65f6\u673a\uff1a\u7ec4\u4ef6\u63a5\u6536\u5230\u65b0\u7684props *\/}\n        \n    \n    \u6253\u8c46\u8c46\n      \n   \n    )\n  }\n}\n\n\/\/ \u5b50\u7ec4\u4ef6\nclass ShowCount extends React.Component {\n  render() {\n    console.warn('\u7ec4\u4ef6ShowCount\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u51fd\u6570\uff1arender')\n    return (<h1>\u7edf\u8ba1\u8c46\u8c46\u88ab\u6253\u7684\u6b21\u6570\uff1a{this.props.count}<\/h1>)\n  }\n\n  \/\/ \u6ce8\u610f\uff1a\u5982\u679c\u8981\u8c03\u7528 setState() \u66f4\u65b0\u72b6\u6001\uff0c\u5fc5\u987b\u8981\u653e\u5728\u4e00\u4e2a if \u6761\u4ef6\u4e2d\n  \/\/ \u56e0\u4e3a\uff1a\u5982\u679c\u76f4\u63a5\u8c03\u7528 setState()\uff0c\u4e5f\u4f1a\u5bfc\u81f4\u9012\u5f52\u66f4\u65b0\uff01\uff01\uff01\n  componentDidUpdate(prevProps) {\n    \/\/ componentDidUpdate\u7684\u4f5c\u7528\uff1a\u83b7\u53d6DOM\n    const title = document.getElementById('title')\n    console.log(title)\n\n    \/\/ \u6b63\u786e\u505a\u6cd5\uff1a\u6bd4\u8f83\u66f4\u65b0\u524d\u540e\u7684props\u662f\u5426\u76f8\u540c\uff0c\u6765\u51b3\u5b9a\u662f\u5426\u91cd\u65b0\u6e32\u67d3\u7ec4\u4ef6\n    console.log('\u4e0a\u4e00\u6b21\u7684props\uff1a', prevProps, '\uff0c\u5f53\u524d\u7684props\uff1a', this.props)\n    if (prevProps.count !== this.props.count) {\n      this.setState({})\n\n      \/\/ componentDidUpdate\u7684\u4f5c\u7528\uff1a\u53d1\u9001ajax\u8bf7\u6c42\u6570\u636e\n      \/\/ axios.get('http:\/\/api....')\n    }\n\n    \/\/ \u9519\u8bef\u6f14\u793a\n    \/\/ this.setState({})\n\n    console.warn('\u7ec4\u4ef6ShowCount\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u51fd\u6570\uff1acomponentDidUpdate')\n  }\n}\nReactDOM.render(\n   , document.getElementById('root'))<\/pre>\n<h3>2.3. \u5378\u8f7d\u65f6\uff08\u5378\u8f7d\u9636\u6bb5\uff09<\/h3>\n<p><strong>\u6267\u884c\u65f6\u673a\uff1a<\/strong>\u7ec4\u4ef6\u4ece\u9875\u9762\u4e2d\u6d88\u5931<\/p>\n<table>\n<tbody>\n<tr>\n<th>\u94a9\u5b50\u51fd\u6570<\/th>\n<th>\u89e6\u53d1\u65f6\u673a<\/th>\n<th>\u4f5c\u7528<\/th>\n<\/tr>\n<tr>\n<td>componentWillUnmount<\/td>\n<td>\u7ec4\u4ef6\u5378\u8f7d\uff08\u4ece\u9875\u9762\u4e2d\u6d88\u5931\uff09<\/td>\n<td>\u6267\u884c\u6e05\u7406\u5de5\u4f5c\uff08\u6bd4\u5982\uff1a\u6e05\u7406\u5b9a\u65f6\u5668\u7b49\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre>\/\/ \u5bfc\u5165ract\nimport React from 'react'\nimport ReactDOM from 'react-dom'\n\n\/\/ \u7236\u7ec4\u4ef6\nclass App extends React.Component {\n  constructor(props) {\n    super(props)\n\n    this.state = {\n      count: 0\n    }\n  }\n\n  \/\/ \u4e8b\u4ef6\u5904\u7406\u7a0b\u5e8f\n  handleClick = () =&gt; {\n    this.setState({\n      count: this.state.count + 1\n    })\n  }\n\n  render() {\n    return (\n      \n   \n    \n        {\n          this.state.count &gt; 5 ? \n    <p>\u8c46\u8c46\u88ab\u6253\u6b7b\u4e86<\/p> : \n    \n        }\n        \n    \u6253\u8c46\u8c46\n      \n   \n    )\n  }\n}\n\n\/\/ \u5b50\u7ec4\u4ef6\nclass ShowCount extends React.Component {\n  componentDidMount() {\n    this.timerId = setInterval(() =&gt; {\n      console.log('\u5b9a\u65f6\u5668\u6b63\u5728\u6267\u884c~')\n    }, 500)\n  }\n\n  render() {\n    return (<h1>\u7edf\u8ba1\u8c46\u8c46\u88ab\u6253\u7684\u6b21\u6570\uff1a{this.props.count}<\/h1>)\n  }\n\n  componentWillUnmount() {\n    console.warn('\u7ec4\u4ef6ShowCount\u7684\u751f\u547d\u5468\u671f\u94a9\u5b50\u51fd\u6570\uff1acomponentWillUnmount')\n\n    \/\/ \u6e05\u7406\u5b9a\u65f6\u5668\n    clearInterval(this.timerId)\n  }\n\n}\nReactDOM.render(\n   , document.getElementById('root'))<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>React&nbsp;\u7ec4\u4ef6\u7684\u5e38\u7528&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201568","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201568"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201568\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}