{"id":5697,"date":"2023-01-06T14:39:59","date_gmt":"2023-01-06T06:39:59","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5697"},"modified":"2023-01-06T14:39:59","modified_gmt":"2023-01-06T06:39:59","slug":"%e4%ba%86%e8%a7%a3react%e7%b5%84%e4%bb%b6%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e4%ba%86%e8%a7%a3react%e7%b5%84%e4%bb%b6%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5\/","title":{"rendered":"\u4e86\u89e3React\u7d44\u4ef6\u751f\u547d\u5468\u671f\u7684\u57fa\u672c\u6982\u5ff5"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, \u751f\u547d\u5468\u671f, \u7d44\u4ef6\"><\/p>\n<p>React \u7d44\u4ef6\u7684\u751f\u547d\u5468\u671f\u662f\u6307\u7d44\u4ef6\u5728\u5b83\u7684\u6574\u500b\u751f\u547d\u9031\u671f\u4e2d\uff0c\u6703\u7d93\u6b77\u54ea\u4e9b\u72c0\u614b\uff0c\u4ee5\u53ca\u5728\u6bcf\u500b\u72c0\u614b\u4e2d\u6703\u505a\u4ec0\u9ebc\u4e8b\u60c5\u3002React \u7d44\u4ef6\u7684\u751f\u547d\u5468\u671f\u53ef\u4ee5\u5206\u70ba\u4e09\u500b\u968e\u6bb5\uff1a\u521d\u59cb\u5316\u3001\u66f4\u65b0\u548c\u6d88\u6ec5\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u521d\u59cb\u5316<\/h2>\n<p>\u521d\u59cb\u5316\u662f React \u7d44\u4ef6\u7684\u7b2c\u4e00\u500b\u968e\u6bb5\uff0c\u5728\u9019\u500b\u968e\u6bb5\uff0cReact \u7d44\u4ef6\u6703\u505a\u4ee5\u4e0b\u4e8b\u60c5\uff1a<\/p>\n<ul>\n<li>\u521d\u59cb\u5316\u7d44\u4ef6\u7684\u72c0\u614b\uff08state\uff09<\/li>\n<li>\u5c07 props \u50b3\u5165\u7d44\u4ef6<\/li>\n<li>\u5c07\u7d44\u4ef6\u6e32\u67d3\u5230 DOM \u4e2d<\/li>\n<\/ul>\n<p>\u5728\u521d\u59cb\u5316\u968e\u6bb5\uff0cReact \u7d44\u4ef6\u6703\u8abf\u7528\u4ee5\u4e0b\u5169\u500b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><code>constructor()<\/code>\uff1a\u5728\u7d44\u4ef6\u88ab\u6e32\u67d3\u4e4b\u524d\uff0c\u6703\u8abf\u7528 <code>constructor()<\/code> \u65b9\u6cd5\uff0c\u5728\u9019\u500b\u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u521d\u59cb\u5316\u7d44\u4ef6\u7684\u72c0\u614b\uff08state\uff09\uff0c\u4ee5\u53ca\u5c07 props \u50b3\u5165\u7d44\u4ef6\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">constructor(props) {\n    super(props);\n    this.state = {\n        name: 'John Doe'\n    };\n}\n<\/code><\/pre>\n<ul>\n<li><code>render()<\/code>\uff1a\u5728 <code>constructor()<\/code> \u65b9\u6cd5\u57f7\u884c\u5b8c\u4e4b\u5f8c\uff0c\u6703\u8abf\u7528 <code>render()<\/code> \u65b9\u6cd5\uff0c\u5728\u9019\u500b\u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u5c07\u7d44\u4ef6\u6e32\u67d3\u5230 DOM \u4e2d\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">render() {\n    return (\n        &lt;div&gt;\n            &lt;h1&gt;Hello, {this.state.name}&lt;\/h1&gt;\n        &lt;\/div&gt;\n    );\n}\n<\/code><\/pre>\n<h2>\u66f4\u65b0<\/h2>\n<p>\u66f4\u65b0\u662f React \u7d44\u4ef6\u7684\u7b2c\u4e8c\u500b\u968e\u6bb5\uff0c\u5728\u9019\u500b\u968e\u6bb5\uff0cReact \u7d44\u4ef6\u6703\u505a\u4ee5\u4e0b\u4e8b\u60c5\uff1a<\/p>\n<ul>\n<li>\u66f4\u65b0\u7d44\u4ef6\u7684\u72c0\u614b\uff08state\uff09<\/li>\n<li>\u66f4\u65b0\u7d44\u4ef6\u7684 props<\/li>\n<li>\u66f4\u65b0\u7d44\u4ef6\u7684 DOM<\/li>\n<\/ul>\n<p>\u5728\u66f4\u65b0\u968e\u6bb5\uff0cReact \u7d44\u4ef6\u6703\u8abf\u7528\u4ee5\u4e0b\u5169\u500b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><code>componentWillReceiveProps()<\/code>\uff1a\u7576\u7d44\u4ef6\u63a5\u6536\u5230\u65b0\u7684 props \u6642\uff0c\u6703\u8abf\u7528 <code>componentWillReceiveProps()<\/code> \u65b9\u6cd5\uff0c\u5728\u9019\u500b\u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u66f4\u65b0\u7d44\u4ef6\u7684 props\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">componentWillReceiveProps(nextProps) {\n    this.setState({\n        name: nextProps.name\n    });\n}\n<\/code><\/pre>\n<ul>\n<li><code>shouldComponentUpdate()<\/code>\uff1a\u7576\u7d44\u4ef6\u7684 props \u6216 state \u88ab\u66f4\u65b0\u6642\uff0c\u6703\u8abf\u7528 <code>shouldComponentUpdate()<\/code> \u65b9\u6cd5\uff0c\u5728\u9019\u500b\u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u6c7a\u5b9a\u7d44\u4ef6\u662f\u5426\u61c9\u8a72\u66f4\u65b0 DOM\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">shouldComponentUpdate(nextProps, nextState) {\n    if (this.state.name !== nextState.name) {\n        return true;\n    }\n    return false;\n}\n<\/code><\/pre>\n<h2>\u6d88\u6ec5<\/h2>\n<p>\u6d88\u6ec5\u662f React \u7d44\u4ef6\u7684\u7b2c\u4e09\u500b\u968e\u6bb5\uff0c\u5728\u9019\u500b\u968e\u6bb5\uff0cReact \u7d44\u4ef6\u6703\u505a\u4ee5\u4e0b\u4e8b\u60c5\uff1a<\/p>\n<ul>\n<li>\u6e05\u9664\u7d44\u4ef6\u7684 DOM<\/li>\n<li>\u6e05\u9664\u7d44\u4ef6\u7684\u4e8b\u4ef6\u76e3\u807d\u5668<\/li>\n<li>\u6e05\u9664\u7d44\u4ef6\u7684\u5b9a\u6642\u5668<\/li>\n<\/ul>\n<p>\u5728\u6d88\u6ec5\u968e\u6bb5\uff0cReact \u7d44\u4ef6\u6703\u8abf\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<ul>\n<li><code>componentWillUnmount()<\/code>\uff1a\u7576\u7d44\u4ef6\u88ab\u5f9e DOM \u4e2d\u79fb\u9664\u6642\uff0c\u6703\u8abf\u7528 <code>componentWillUnmount()<\/code> \u65b9\u6cd5\uff0c\u5728\u9019\u500b\u65b9\u6cd5\u4e2d\uff0c\u53ef\u4ee5\u6e05\u9664\u7d44\u4ef6\u7684\u4e8b\u4ef6\u76e3\u807d\u5668\u548c\u5b9a\u6642\u5668\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-javascript line-numbers\">componentWillUnmount() {\n    clearInterval(this.timer);\n    document.removeEventListener('click', this.handleClick);\n}\n<\/code><\/pre>\n<p>React \u7d44\u4ef6\u7684\u751f\u547d\u5468\u671f\u662f React \u7d44\u4ef6\u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u597d\u5730\u63a7\u5236\u7d44\u4ef6\u7684\u884c\u70ba\uff0c\u4e26\u4e14\u66f4\u597d\u5730\u7ba1\u7406\u7d44\u4ef6\u7684\u8cc7\u6e90\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aReact\u7d44\u4ef6\u7684\u751f\u547d\u5468\u671f\u662f\u6307\u7d44\u4ef6\u5728\u61c9\u7528\u7a0b\u5e8f\u4e2d\u7684\u751f\u547d\u9031\u671f\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7a2e\u65b9\u6cd5\u4f86\u63a7\u5236\u7d44\u4ef6\u7684\u884c\u70ba\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u7a2e\u65b9\u6cd5\u4f86\u66f4\u65b0\u7d44\u4ef6\u7684\u72c0\u614b\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39React\u7d44\u4ef6\u7684\u751f\u547d\u5468\u671f\uff0c\u4e26\u89e3\u91cb\u5b83\u5011\u5982\u4f55\u5f71\u97ff\u7d44\u4ef6\u7684\u884c\u70ba\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-5697","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1tT","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5697"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5697\/revisions"}],"predecessor-version":[{"id":5698,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5697\/revisions\/5698"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}