{"id":1278,"date":"2024-07-29T00:59:18","date_gmt":"2024-07-28T16:59:18","guid":{"rendered":"http:\/\/codermr.com\/?p=1278"},"modified":"2024-08-04T01:55:42","modified_gmt":"2024-08-03T17:55:42","slug":"react_note_summary1","status":"publish","type":"post","link":"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/","title":{"rendered":"React \u77e5\u8bc6\u70b9\u603b\u7ed3"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u591a\u4e2a JSX \u6807\u7b7e\u9700\u8981\u88ab\u4e00\u4e2a\u7236\u5143\u7d20\u5305\u88f9\uff1f<\/h1>\n\n\n\n<p>JSX \u867d\u7136\u770b\u8d77\u6765\u5f88\u50cf HTML\uff0c\u4f46\u5728\u5e95\u5c42\u5176\u5b9e\u88ab\u8f6c\u5316\u4e3a\u4e86 JavaScript \u5bf9\u8c61\uff0c\u4f60\u4e0d\u80fd\u5728\u4e00\u4e2a\u51fd\u6570\u4e2d\u8fd4\u56de\u591a\u4e2a\u5bf9\u8c61\uff0c\u9664\u975e\u7528\u4e00\u4e2a\u6570\u7ec4\u628a\u4ed6\u4eec\u5305\u88c5\u8d77\u6765\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u591a\u4e2a JSX \u6807\u7b7e\u5fc5\u987b\u8981\u7528\u4e00\u4e2a\u7236\u5143\u7d20\u6216\u8005 Fragment \u6765\u5305\u88f9\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u795e\u5947\u7684\u5927\u62ec\u53f7<\/h1>\n\n\n\n<p>\u5f53\u4f60\u60f3\u628a\u4e00\u4e2a\u5b57\u7b26\u4e32\u5c5e\u6027\u4f20\u9012\u7ed9 JSX \u65f6\uff0c\u628a\u5b83\u653e\u5230\u5355\u5f15\u53f7\u6216\u53cc\u5f15\u53f7\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default function Avatar() {\n  return (\n    &lt;img\n      className=\"avatar\"\n      src=\"https:\/\/i.imgur.com\/7vQD0fPs.jpg\"\n      alt=\"Gregorio Y. Zara\"\n    \/>\n  );\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684&#8221;<a href=\"https:\/\/i.imgur.com\/7vQD0fPs.jpg\">https:\/\/i.imgur.com\/7vQD0fPs.jpg<\/a>&#8220;\u548c&#8221;Gregorio Y. Zara&#8221;\u5c31\u662f\u88ab\u4f5c\u4e3a\u5b57\u7b26\u4e32\u4f20\u9012\u7684\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u5982\u679c\u4f60\u60f3\u8981\u52a8\u6001\u5730\u6307\u5b9asrc\u6216alt\u7684\u503c\u5462\uff1f\u4f60\u53ef\u4ee5<strong>\u7528<\/strong><strong>{<\/strong><strong>\u548c<\/strong><strong>}<\/strong><strong>\u66ff\u4ee3<\/strong><strong>&#8220;<\/strong><strong>\u548c<\/strong><strong>&#8220;<\/strong><strong>\u4ee5\u4f7f\u7528 JavaScript \u53d8\u91cf<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default function Avatar() {\n  const avatar = 'https:\/\/i.imgur.com\/7vQD0fPs.jpg';\n  const description = 'Gregorio Y. Zara';\n  return (\n    &lt;img\n      className=\"avatar\"\n      src={avatar}\n      alt={description}\n    \/>\n  );\n}<\/code><\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f className=&#8221;avatar&#8221; \u548c src={avatar} \u4e4b\u95f4\u7684\u533a\u522b\uff0cclassName=&#8221;avatar&#8221; \u6307\u5b9a\u4e86\u4e00\u4e2a\u5c31\u53eb &#8220;avatar&#8221; \u7684\u4f7f\u56fe\u7247\u5728\u6837\u5f0f\u4e0a\u53d8\u5706\u7684 CSS \u7c7b\u540d\uff0c\u800c src={avatar} \u8fd9\u79cd\u5199\u6cd5\u4f1a\u53bb\u8bfb\u53d6 JavaScript \u4e2d avatar \u8fd9\u4e2a\u53d8\u91cf\u7684\u503c\u3002\u8fd9\u662f\u56e0\u4e3a\u5927\u62ec\u53f7\u53ef\u4ee5\u4f7f\u4f60\u76f4\u63a5\u5728\u6807\u7b7e\u4e2d\u4f7f\u7528 JavaScript\uff01<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4f7f\u7528 props \u4e92\u76f8\u901a\u4fe1<\/h1>\n\n\n\n<p>React \u7ec4\u4ef6\u4f7f\u7528 props \u6765\u4e92\u76f8\u901a\u4fe1\u3002\u6bcf\u4e2a\u7236\u7ec4\u4ef6\u90fd\u53ef\u4ee5\u63d0\u4f9b props \u7ed9\u5b83\u7684\u5b50\u7ec4\u4ef6\uff0c\u4ece\u800c\u5c06\u4e00\u4e9b\u4fe1\u606f\u4f20\u9012\u7ed9\u5b83\u3002Props \u53ef\u80fd\u4f1a\u8ba9\u4f60\u60f3\u8d77 HTML \u5c5e\u6027\uff0c\u4f46\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b83\u4eec\u4f20\u9012\u4efb\u4f55 JavaScript \u503c\uff0c\u5305\u62ec\u5bf9\u8c61\u3001\u6570\u7ec4\u548c\u51fd\u6570\u3002<\/p>\n\n\n\n<p>Props \u662f\u4f60\u4f20\u9012\u7ed9 JSX \u6807\u7b7e\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff0cclassName\u3001src\u3001alt\u3001width \u548c height \u4fbf\u662f\u4e00\u4e9b\u53ef\u4ee5\u4f20\u9012\u7ed9 &lt;img> \u7684 props\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Avatar() {\n  return (\n    &lt;img\n      className=\"avatar\"\n      src=\"https:\/\/i.imgur.com\/1bX5QH6.jpg\"\n      alt=\"Lin Lanying\"\n      width={100}\n      height={100}\n    \/>\n  );\n}\n\n\nexport default function Profile() {\n  return (\n    &lt;Avatar \/>\n  );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6b65\u9aa4 1: \u5c06 props \u4f20\u9012\u7ed9\u5b50\u7ec4\u4ef6<\/h2>\n\n\n\n<p>\u9996\u5148\uff0c\u5c06\u4e00\u4e9b props \u4f20\u9012\u7ed9 Avatar\u3002\u4f8b\u5982\uff0c\u8ba9\u6211\u4eec\u4f20\u9012\u4e24\u4e2a props\uff1aperson\uff08\u4e00\u4e2a\u5bf9\u8c61\uff09\u548c size\uff08\u4e00\u4e2a\u6570\u5b57\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default function Profile() {\n    return (\n        &lt;Avatar\n        person={{ name: 'Lin Lanying', imageId: '1bX5QH6' }}\n        size={100}\n        \/>\n    );\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6ce8\u610f<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c person=\u540e\u9762\u7684\u53cc\u82b1\u62ec\u53f7\u8ba9\u4f60\u611f\u5230\u56f0\u60d1\uff0c\u8bf7\u8bb0\u4f4f\uff0c\u5728 JSX \u82b1\u62ec\u53f7\u4e2d\uff0c<a href=\"https:\/\/react.docschina.org\/learn\/javascript-in-jsx-with-curly-braces#using-double-curlies-css-and-other-objects-in-jsx\">\u5b83\u4eec\u53ea\u662f\u4e00\u4e2a\u5bf9\u8c61<\/a>\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5728Avatar\u7ec4\u4ef6\u4e2d\u8bfb\u53d6\u8fd9\u4e9b props \u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6b65\u9aa4 2: \u5728\u5b50\u7ec4\u4ef6\u4e2d\u8bfb\u53d6 props<\/strong><strong>&nbsp;<\/strong><\/h2>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728function Avatar\u4e4b\u540e\u76f4\u63a5\u5217\u51fa\u5b83\u4eec\u7684\u540d\u5b57person, size\u6765\u8bfb\u53d6\u8fd9\u4e9b props\u3002\u8fd9\u4e9b props \u5728({\u548c})\u4e4b\u95f4\uff0c\u5e76\u7531\u9017\u53f7\u5206\u9694\u3002\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u5728Avatar\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528\u5b83\u4eec\uff0c\u5c31\u50cf\u4f7f\u7528\u53d8\u91cf\u4e00\u6837\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Avatar({ person, size }) {\n\/\/ \u5728\u8fd9\u91cc person \u548c size \u662f\u53ef\u8bbf\u95ee\u7684\n}<\/code><\/pre>\n\n\n\n<p>\u5411\u4f7f\u7528 person \u548c size props \u6e32\u67d3\u7684 Avatar \u6dfb\u52a0\u4e00\u4e9b\u903b\u8f91\uff0c\u4f60\u5c31\u5b8c\u6210\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce8\u610f<\/h3>\n\n\n\n<p>\u5728\u58f0\u660e props \u65f6\uff0c <strong>\u4e0d\u8981\u5fd8\u8bb0<\/strong> <strong>(<\/strong> <strong>\u548c<\/strong> <strong>)<\/strong> <strong>\u4e4b\u95f4\u7684\u4e00\u5bf9\u82b1\u62ec\u53f7<\/strong> <strong>{<\/strong> <strong>\u548c<\/strong> <strong>}<\/strong> \uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Avatar({ person, size }) {\n\/\/ ...\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>state \u5982\u540c\u4e00\u5f20\u5feb\u7167<\/strong><\/h1>\n\n\n\n<p>\u4e5f\u8bb8 state \u53d8\u91cf\u770b\u8d77\u6765\u548c\u4e00\u822c\u7684\u53ef\u8bfb\u5199\u7684 JavaScript \u53d8\u91cf\u7c7b\u4f3c\u3002\u4f46 state \u5728\u5176\u8868\u73b0\u51fa\u7684\u7279\u6027\u4e0a\u66f4\u50cf\u662f\u4e00\u5f20\u5feb\u7167\u3002<strong>\u8bbe\u7f6e\u5b83\u4e0d\u4f1a\u66f4\u6539\u4f60\u5df2\u6709\u7684 state \u53d8\u91cf\uff0c\u4f46\u4f1a\u89e6\u53d1\u91cd\u65b0\u6e32\u67d3\u3002<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u8bbe\u7f6e state \u4f1a\u89e6\u53d1\u6e32\u67d3<\/strong><strong>&nbsp;<\/strong><\/h2>\n\n\n\n<p>\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u4f60\u7684\u7528\u6237\u754c\u9762\u4f1a\u76f4\u63a5\u5bf9\u70b9\u51fb\u4e4b\u7c7b\u7684\u7528\u6237\u8f93\u5165\u505a\u51fa\u54cd\u5e94\u5e76\u53d1\u751f\u53d8\u5316\u3002\u5728 React \u4e2d\uff0c\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u4e0e\u8fd9\u79cd\u601d\u7ef4\u6a21\u578b\u7565\u6709\u4e0d\u540c\u3002\u5728\u4e0a\u4e00\u9875\u4e2d\uff0c\u4f60\u770b\u5230\u4e86\u6765\u81ea React \u7684<a href=\"https:\/\/react.docschina.org\/learn\/render-and-commit#step-1-trigger-a-render\">\u8bbe\u7f6e state \u8bf7\u6c42\u91cd\u65b0\u6e32\u67d3<\/a>\u3002\u8fd9\u610f\u5473\u7740\u8981\u4f7f\u754c\u9762\u5bf9\u8f93\u5165\u505a\u51fa\u53cd\u5e94\uff0c\u4f60\u9700\u8981\u8bbe\u7f6e\u5176 state\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u5f53\u4f60\u6309\u4e0b \u201csend\u201d \u65f6\uff0csetIsSent(true) \u4f1a\u901a\u77e5 React \u91cd\u65b0\u6e32\u67d3 UI\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\n\nexport default function Form() {\n  const &#91;isSent, setIsSent] = useState(false);\n  const &#91;message, setMessage] = useState('Hi!');\n  if (isSent) {\n    return &lt;h1>Your message is on its way!&lt;\/h1>\n  }\n  return (\n    &lt;form onSubmit={(e) => {\n      e.preventDefault();\n      setIsSent(true);\n      sendMessage(message);\n    }}>\n      &lt;textarea\n        placeholder=\"Message\"\n        value={message}\n        onChange={e => setMessage(e.target.value)}\n      \/>\n      &lt;button type=\"submit\">Send&lt;\/button>\n    &lt;\/form>\n  );\n}\n\nfunction sendMessage(message) {\n  \/\/ ...\n}<\/code><\/pre>\n\n\n\n<p>\u5f53\u4f60\u5355\u51fb\u6309\u94ae\u65f6\u4f1a\u53d1\u751f\u4ee5\u4e0b\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ol>\n<li>\u6267\u884c\u00a0onSubmit\u00a0\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002<\/li>\n\n\n\n<li>setIsSent(true)\u00a0\u5c06\u00a0isSent\u00a0\u8bbe\u7f6e\u4e3a\u00a0true\u00a0\u5e76\u6392\u5217\u4e00\u4e2a\u65b0\u7684\u6e32\u67d3\u3002<\/li>\n\n\n\n<li>React \u6839\u636e\u65b0\u7684\u00a0isSent\u00a0\u503c\u91cd\u65b0\u6e32\u67d3\u7ec4\u4ef6\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u8ba9\u6211\u4eec\u4ed4\u7ec6\u770b\u770b state \u548c\u6e32\u67d3\u4e4b\u95f4\u7684\u5173\u7cfb\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6e32\u67d3\u4f1a\u53ca\u65f6\u751f\u6210\u4e00\u5f20\u5feb\u7167<\/strong><strong>&nbsp;<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/react.docschina.org\/learn\/render-and-commit#step-2-react-renders-your-components\">\u201c\u6b63\u5728\u6e32\u67d3\u201d<\/a>\u5c31\u610f\u5473\u7740 React \u6b63\u5728\u8c03\u7528\u4f60\u7684\u7ec4\u4ef6\u2014\u2014\u4e00\u4e2a\u51fd\u6570\u3002\u4f60\u4ece\u8be5\u51fd\u6570\u8fd4\u56de\u7684 JSX \u5c31\u50cf\u662f UI \u7684\u4e00\u5f20\u53ca\u65f6\u7684\u5feb\u7167\u3002<strong>\u5b83\u7684 props\u3001\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u548c\u5185\u90e8\u53d8\u91cf\u90fd\u662f<\/strong><strong>\u6839\u636e\u5f53\u524d\u6e32\u67d3\u65f6\u7684 state<\/strong><strong>\u88ab\u8ba1\u7b97\u51fa\u6765\u7684\u3002<\/strong><\/p>\n\n\n\n<p>\u4e0e\u7167\u7247\u6216\u7535\u5f71\u753b\u9762\u4e0d\u540c\uff0c\u4f60\u8fd4\u56de\u7684 UI \u201c\u5feb\u7167\u201d\u662f\u53ef\u4ea4\u4e92\u7684\u3002\u5b83\u5176\u4e2d\u5305\u62ec\u7c7b\u4f3c\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u7684\u903b\u8f91\uff0c\u8fd9\u4e9b\u903b\u8f91\u7528\u4e8e\u6307\u5b9a\u5982\u4f55\u5bf9\u8f93\u5165\u4f5c\u51fa\u54cd\u5e94\u3002React \u968f\u540e\u4f1a\u66f4\u65b0\u5c4f\u5e55\u6765\u5339\u914d\u8fd9\u5f20\u5feb\u7167\uff0c\u5e76\u7ed1\u5b9a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002\u56e0\u6b64\uff0c\u6309\u4e0b\u6309\u94ae\u5c31\u4f1a\u89e6\u53d1\u4f60 JSX \u4e2d\u7684\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002<\/p>\n\n\n\n<p><strong>\u5f53 React \u91cd\u65b0\u6e32\u67d3\u4e00\u4e2a\u7ec4\u4ef6\u65f6\uff1a<\/strong><\/p>\n\n\n\n<ol>\n<li>React \u4f1a\u518d\u6b21\u8c03\u7528\u4f60\u7684\u51fd\u6570<\/li>\n\n\n\n<li>\u4f60\u7684\u51fd\u6570\u4f1a\u8fd4\u56de\u65b0\u7684 JSX \u5feb\u7167<\/li>\n\n\n\n<li>React \u4f1a\u66f4\u65b0\u754c\u9762\u6765\u5339\u914d\u4f60\u8fd4\u56de\u7684\u5feb\u7167<\/li>\n<\/ol>\n\n\n\n<p style=\"font-size:18px\"><br><span><span style=\"letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; font-size: 11pt; color: rgb(35, 39, 47); font-family: &quot;Optimistic Text&quot;, -apple-system, ui-sans-serif, system-ui, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Helvetica Neue&quot;, Arial, &quot;Noto Sans&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Noto Color Emoji&quot;; font-variant-caps: normal; font-variant-ligatures: normal;-en-clipboard:true;\">\u4f5c\u4e3a\u4e00\u4e2a\u7ec4\u4ef6\u7684\u8bb0\u5fc6\uff0cstate \u4e0d\u540c\u4e8e\u5728\u4f60\u7684\u51fd\u6570\u8fd4\u56de\u4e4b\u540e\u5c31\u4f1a\u6d88\u5931\u7684\u666e\u901a\u53d8\u91cf\u3002state \u5b9e\u9645\u4e0a\u201c\u6d3b\u201d\u5728 React \u672c\u8eab\u4e2d\u2014\u2014\u5c31\u50cf\u88ab\u6446\u5728\u4e00\u4e2a\u67b6\u5b50\u4e0a\uff01\u2014\u2014\u4f4d\u4e8e\u4f60\u7684\u51fd\u6570\u4e4b\u5916\u3002\u5f53 React \u8c03\u7528\u4f60\u7684\u7ec4\u4ef6\u65f6\uff0c\u5b83\u4f1a\u4e3a\u7279\u5b9a\u7684\u90a3\u4e00\u6b21\u6e32\u67d3\u63d0\u4f9b\u4e00\u5f20 state \u5feb\u7167\u3002<span style=\"background-color: rgb(255, 250, 165); font-size: 11pt; color: rgb(35, 39, 47); font-family: &quot;Optimistic Text&quot;, -apple-system, ui-sans-serif, system-ui, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Helvetica Neue&quot;, Arial, &quot;Noto Sans&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Noto Color Emoji&quot;; font-variant-caps: normal; font-variant-ligatures: normal;\">\u4f60\u7684\u7ec4\u4ef6\u4f1a\u5728\u5176 JSX \u4e2d\u8fd4\u56de\u4e00\u5f20\u5305\u542b\u4e00\u6574\u5957\u65b0\u7684 props \u548c\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u7684 UI \u5feb\u7167 \uff0c\u5176\u4e2d\u6240\u6709\u7684\u503c\u90fd\u662f<\/span><\/span> <span style=\"background-color: rgb(255, 250, 165);\"><span style=\"box-sizing: border-box; border: 0px solid rgb(229, 231, 235); --tw-translate-x: 0; --tw-translate-y: 0; --tw-rotate: 0; --tw-skew-x: 0; --tw-skew-y: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-ring-offset-width: 0px; --tw-ring-offset-color: #fff; --tw-ring-color: rgba(59,130,246,0.5); --tw-ring-offset-shadow: 0 0 #0000; --tw-ring-shadow: 0 0 #0000; --tw-shadow: 0 0 #0000; --tw-shadow-colored: 0 0 #0000; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; font-size: 11pt; background-color: rgb(255, 250, 165); color: rgb(35, 39, 47); font-family: &quot;Optimistic Text&quot;, -apple-system, ui-sans-serif, system-ui, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Helvetica Neue&quot;, Arial, &quot;Noto Sans&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Noto Color Emoji&quot;; font-variant-caps: normal; font-variant-ligatures: normal; font-weight: bold;\">\u6839\u636e\u90a3\u4e00\u6b21\u6e32\u67d3\u4e2d state \u7684\u503c<\/span> <span style=\"letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; font-size: 11pt; background-color: rgb(255, 250, 165); color: rgb(35, 39, 47); font-family: &quot;Optimistic Text&quot;, -apple-system, ui-sans-serif, system-ui, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Helvetica Neue&quot;, Arial, &quot;Noto Sans&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Noto Color Emoji&quot;; font-variant-caps: normal; font-variant-ligatures: normal;\">\u88ab\u8ba1\u7b97\u51fa\u6765\u7684\uff01<\/span><\/span><\/span><br><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">React \u53d7\u5230 setUpdate \u901a\u77e5 &#8212;>  React \u66f4\u65b0 state \u7684\u503c  &#8212;>  React \u5411\u7ec4\u4ef6\u5185\u4f20\u5165\u4e00\u5f20 state \u7684\u5feb\u7167<\/mark><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6709\u8da3\u7684\u4f8b\u5b50<\/h2>\n\n\n\n<p>\u8fd9\u91cc\u6709\u4e2a\u5411\u4f60\u5c55\u793a\u5176\u8fd0\u884c\u539f\u7406\u7684\u5c0f\u4f8b\u5b50\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u4ee5\u4e3a\u70b9\u51fb\u201c+3\u201d\u6309\u94ae\u4f1a\u8c03\u7528setNumber(number + 1)\u4e09\u6b21\u4ece\u800c\u4f7f\u8ba1\u6570\u5668\u9012\u589e\u4e09\u6b21\u3002<\/p>\n\n\n\n<p>\u770b\u770b\u4f60\u70b9\u51fb\u201c+3\u201d\u6309\u94ae\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\n\nexport default function Counter() {\n  const &#91;number, setNumber] = useState(0);\n\n  return (\n    &lt;>\n      &lt;h1>{number}&lt;\/h1>\n      &lt;button onClick={() => {\n        setNumber(number + 1);\n        setNumber(number + 1);\n        setNumber(number + 1);\n      }}>+3&lt;\/button>\n    &lt;\/>\n  )\n}<\/code><\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u6bcf\u6b21\u70b9\u51fb\u53ea\u4f1a\u8ba9number\u9012\u589e\u4e00\u6b21\uff01<\/p>\n\n\n\n<p><strong>\u8bbe\u7f6e state \u53ea\u4f1a\u4e3a\u4e0b\u4e00\u6b21\u6e32\u67d3\u53d8\u66f4 state \u7684\u503c<\/strong>\u3002\u5728\u7b2c\u4e00\u6b21\u6e32\u67d3\u671f\u95f4\uff0cnumber\u4e3a0\u3002\u8fd9\u4e5f\u5c31\u89e3\u91ca\u4e86\u4e3a\u4ec0\u4e48\u5728<strong>\u90a3\u6b21\u6e32\u67d3\u4e2d\u7684<\/strong>onClick\u5904\u7406\u51fd\u6570\u4e2d\uff0c\u5373\u4fbf\u5728\u8c03\u7528\u4e86setNumber(number + 1)\u4e4b\u540e\uff0cnumber\u7684\u503c\u4e5f\u4ecd\u7136\u662f0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button onClick={() => {\n        setNumber(number + 1);\n        setNumber(number + 1);\n        setNumber(number + 1);\n    }}> +3\n&lt;\/button><\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u8fd9\u4e2a\u6309\u94ae\u7684\u70b9\u51fb\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u901a\u77e5 React \u8981\u505a\u7684\u4e8b\u60c5\uff1a<\/p>\n\n\n\n<ol>\n<li>setNumber(number + 1)\uff1anumber\u00a0\u662f\u00a00\u00a0\u6240\u4ee5\u00a0setNumber(0 + 1)\u3002\n<ul>\n<li>React \u51c6\u5907\u5728\u4e0b\u4e00\u6b21\u6e32\u67d3\u65f6\u5c06\u00a0number\u00a0\u66f4\u6539\u4e3a\u00a01\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>setNumber(number + 1)\uff1anumber\u00a0\u662f0\u00a0\u6240\u4ee5\u00a0setNumber(0 + 1)\u3002\n<ul>\n<li>React \u51c6\u5907\u5728\u4e0b\u4e00\u6b21\u6e32\u67d3\u65f6\u5c06\u00a0number\u00a0\u66f4\u6539\u4e3a\u00a01\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>setNumber(number + 1)\uff1anumber\u00a0\u662f0\u00a0\u6240\u4ee5\u00a0setNumber(0 + 1)\u3002\n<ul>\n<li>React \u51c6\u5907\u5728\u4e0b\u4e00\u6b21\u6e32\u67d3\u65f6\u5c06\u00a0number\u00a0\u66f4\u6539\u4e3a\u00a01\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>\u5c3d\u7ba1\u4f60\u8c03\u7528\u4e86\u4e09\u6b21setNumber(number + 1)\uff0c\u4f46\u5728<strong>\u8fd9\u6b21\u6e32\u67d3\u7684<\/strong>\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2dnumber\u4f1a\u4e00\u76f4\u662f0\uff0c\u6240\u4ee5\u4f60\u4f1a\u4e09\u6b21\u5c06 state \u8bbe\u7f6e\u62101\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5728\u4f60\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u6267\u884c\u5b8c\u4ee5\u540e\uff0cReact \u91cd\u65b0\u6e32\u67d3\u7684\u7ec4\u4ef6\u4e2d\u7684number\u7b49\u4e8e1\u800c\u4e0d\u662f3\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-69.png\"><img loading=\"lazy\" decoding=\"async\" width=\"3522\" height=\"1535\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-69.png\" alt=\"\" class=\"wp-image-1279\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-69.png 3522w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-69-300x131.png 300w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-69-1024x446.png 1024w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-69-768x335.png 768w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-69-1536x669.png 1536w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-69-2048x893.png 2048w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-69-1222x533.png 1222w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-69-922x402.png 922w\" sizes=\"(max-width: 3522px) 100vw, 3522px\" \/><\/a><\/figure>\n\n\n\n<p>\u2588\u2588<strong>React \u4f1a\u7b49\u5230\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u7684<\/strong>\u6240\u6709<strong>\u4ee3\u7801\u90fd\u8fd0\u884c\u5b8c\u6bd5\u518d\u5904\u7406\u4f60\u7684 state \u66f4\u65b0\u3002<\/strong>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u91cd\u65b0\u6e32\u67d3\u53ea\u4f1a\u53d1\u751f\u5728\u6240\u6709\u8fd9\u4e9bsetNumber()\u8c03\u7528<strong>\u4e4b\u540e<\/strong>\u7684\u539f\u56e0\u3002\u2588\u2588<\/p>\n\n\n\n<p>\u2588\u2588\u5982\u679c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u4f60\u60f3\u8c03\u7528\u4e09\u6b21setNumber\u65b9\u6cd5\u540e\u8fbe\u5230state\u4e2dnumber\u7684\u503c\u53d8\u62103\uff0c\u5219\uff1a\u5982\u679c\u4f60\u60f3\u5728\u4e0b\u6b21\u6e32\u67d3\u4e4b\u524d\u591a\u6b21\u66f4\u65b0\u540c\u4e00\u4e2a state\uff0c\u4f60\u53ef\u4ee5\u50cfsetNumber(n =&gt; n + 1)\u8fd9\u6837\u4f20\u5165\u4e00\u4e2a\u6839\u636e\u961f\u5217\u4e2d\u7684\u524d\u4e00\u4e2a state \u8ba1\u7b97\u4e0b\u4e00\u4e2a state \u7684<strong>\u51fd\u6570<\/strong>\uff0c\u800c\u4e0d\u662f\u50cfsetNumber(number + 1)\u8fd9\u6837\u4f20\u5165<strong>\u4e0b\u4e00\u4e2a state \u503c<\/strong>\u3002\u2588\u2588<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u968f\u65f6\u95f4\u53d8\u5316\u7684 state<\/strong><strong>&nbsp;<\/strong><\/h2>\n\n\n\n<p>\u597d\u7684\uff0c\u521a\u624d\u90a3\u4e9b\u5f88\u6709\u610f\u601d\u3002\u8bd5\u7740\u731c\u731c\u70b9\u51fb\u8fd9\u4e2a\u6309\u94ae\u4f1a\u53d1\u51fa\u4ec0\u4e48\u8b66\u544a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\n\nexport default function Counter() {\n  const &#91;number, setNumber] = useState(0);\n\n  return (\n    &lt;>\n      &lt;h1>{number}&lt;\/h1>\n      &lt;button onClick={() => {\n        setNumber(number + 5);\n        alert(number);\n      }}>+5&lt;\/button>\n    &lt;\/>\n  )\n}<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u4f7f\u7528\u4e4b\u524d\u66ff\u6362\u7684\u65b9\u6cd5\uff0c\u4f60\u5c31\u80fd\u731c\u5230\u8fd9\u4e2a\u63d0\u793a\u6846\u5c06\u4f1a\u663e\u793a \u201c0\u201d<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u3010\u786e\u5b9e\u662f0\uff0c&lt;button..\/>\u91cc\u9762\u7684setNumber(number+5)\u548calert(number)\u8bfb\u53d6\u5230\u7684number\u90fd\u662f0\u3011<\/mark><\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>setNumber(0 + 5);\nalert(0);<\/code><\/pre>\n\n\n\n<p>\u4f46\u5982\u679c\u4f60\u5728\u8fd9\u4e2a\u63d0\u793a\u6846\u4e0a\u52a0\u4e0a\u4e00\u4e2a\u5b9a\u65f6\u5668\uff0c \u4f7f\u5f97\u5b83\u5728\u7ec4\u4ef6\u91cd\u65b0\u6e32\u67d3 <strong>\u4e4b\u540e<\/strong> \u624d\u89e6\u53d1\uff0c\u53c8\u4f1a\u600e\u6837\u5462\uff1f\u662f\u4f1a\u663e\u793a \u201c0\u201d \u8fd8\u662f \u201c5\u201d \uff1f\u731c\u4e00\u731c\uff01<strong>\u3010&lt;h1..\/>\u6807\u7b7e\u91cc\u663e\u793a\u7684\u662f5\uff0calert\u5f39\u7a97\u91cc\u9762\u662f0\uff0c\u56e0\u4e3a\u5728\u7b2cN\u6b21\u6e32\u67d3\u4e2dalert\u8bfb\u5230\u7684\u503c\u662f0\u90a3\u4e48\u5373\u4f7f\u5ef6\u8fdf\u5f39\u7a97\uff0c\u90a3\u4e2a\u503c\u8fd8\u662f5\u3011<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\n\nexport default function Counter() {\n  const &#91;number, setNumber] = useState(0);\n\n  return (\n    &lt;>\n      &lt;h1>{number}&lt;\/h1>\n      &lt;button onClick={() => {\n        setNumber(number + 5);\n        setTimeout(() => {\n          alert(number);\n        }, 3000);\n      }}>+5&lt;\/button>\n    &lt;\/>\n  )\n}<\/code><\/pre>\n\n\n\n<p>\u60ca\u8bb6\u5417\uff1f\u4f60\u5982\u679c\u4f7f\u7528\u66ff\u4ee3\u6cd5\uff0c\u5c31\u80fd\u770b\u5230\u88ab\u4f20\u5165\u63d0\u793a\u6846\u7684 state \u201c\u5feb\u7167\u201d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>setNumber(0 + 5);\nsetTimeout(() => {\nalert(0);\n}, 3000);<\/code><\/pre>\n\n\n\n<p>\u5230\u63d0\u793a\u6846\u8fd0\u884c\u65f6\uff0cReact \u4e2d\u5b58\u50a8\u7684 state \u53ef\u80fd\u5df2\u7ecf\u53d1\u751f\u4e86\u66f4\u6539\uff0c\u4f46\u5b83\u662f\u4f7f\u7528\u7528\u6237\u4e0e\u4e4b\u4ea4\u4e92\u65f6\u72b6\u6001\u7684\u5feb\u7167\u8fdb\u884c\u8c03\u5ea6\u7684\uff01<\/p>\n\n\n\n<p><strong>\u4e00\u4e2a state \u53d8\u91cf\u7684\u503c\u6c38\u8fdc\u4e0d\u4f1a\u5728\u4e00\u6b21\u6e32\u67d3\u7684\u5185\u90e8\u53d1\u751f\u53d8\u5316\uff0c<\/strong>\u5373\u4f7f\u5176\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u7684\u4ee3\u7801\u662f\u5f02\u6b65\u7684\u3002\u5728<strong>\u90a3\u6b21\u6e32\u67d3\u7684<\/strong>onClick\u5185\u90e8\uff0cnumber\u7684\u503c\u5373\u4f7f\u5728\u8c03\u7528setNumber(number + 5)\u4e4b\u540e\u4e5f\u8fd8\u662f0\u3002\u5b83\u7684\u503c\u5728 React \u901a\u8fc7\u8c03\u7528\u4f60\u7684\u7ec4\u4ef6\u201c\u83b7\u53d6 UI \u7684\u5feb\u7167\u201d\u65f6\u5c31\u88ab\u201c\u56fa\u5b9a\u201d\u4e86\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6709\u4e2a\u793a\u4f8b\u80fd\u591f\u8bf4\u660e\u4e0a\u8ff0\u7279\u6027\u4f1a\u4f7f\u4f60\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u66f4\u4e0d\u5bb9\u6613\u51fa\u73b0\u8ba1\u65f6\u9519\u8bef\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f1a\u5728\u4e94\u79d2\u5ef6\u8fdf\u4e4b\u540e\u53d1\u9001\u4e00\u6761\u6d88\u606f\u7684\u8868\u5355\u3002\u60f3\u8c61\u4ee5\u4e0b\u573a\u666f\uff1a<\/p>\n\n\n\n<ol>\n<li>\u4f60\u6309\u4e0b\u201c\u53d1\u9001\u201d\u6309\u94ae\uff0c\u5411 Alice \u53d1\u9001\u201c\u4f60\u597d\u201d\u3002<\/li>\n\n\n\n<li>\u5728\u4e94\u79d2\u5ef6\u8fdf\u7ed3\u675f\u4e4b\u524d\uff0c\u5c06\u201cTo\u201d\u5b57\u6bb5\u7684\u503c\u66f4\u6539\u4e3a\u201cBob\u201d\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4f60\u89c9\u5f97alert\u4f1a\u663e\u793a\u4ec0\u4e48\uff1f\u5b83\u662f\u4f1a\u663e\u793a\u201c\u4f60\u5411 Alice \u8bf4\u4e86\u4f60\u597d\u201c\u8fd8\u662f\u4f1a\u663e\u793a\u201c\u4f60\u5411 Bob \u8bf4\u4e86\u4f60\u597d\u201d\uff1f\u6839\u636e\u4f60\u5df2\u7ecf\u5b66\u5230\u7684\u77e5\u8bc6\u731c\u4e00\u731c\uff0c\u7136\u540e\u52a8\u624b\u8bd5\u4e00\u8bd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\n\nexport default function Form() {\n  const &#91;to, setTo] = useState('Alice');\n  const &#91;message, setMessage] = useState('Hello');\n\n  function handleSubmit(e) {\n    e.preventDefault();\n    setTimeout(() => {\n      alert(`You said ${message} to ${to}`);\n    }, 5000);\n  }\n\n  return (\n    &lt;form onSubmit={handleSubmit}>\n      &lt;label>\n        To:{' '}\n        &lt;select\n          value={to}\n          onChange={e => setTo(e.target.value)}>\n          &lt;option value=\"Alice\">Alice&lt;\/option>\n          &lt;option value=\"Bob\">Bob&lt;\/option>\n        &lt;\/select>\n      &lt;\/label>\n      &lt;textarea\n        placeholder=\"Message\"\n        value={message}\n        onChange={e => setMessage(e.target.value)}\n      \/>\n      &lt;button type=\"submit\">Send&lt;\/button>\n    &lt;\/form>\n  );\n}<\/code><\/pre>\n\n\n\n<p><strong>React \u4f1a\u4f7f state \u7684\u503c\u59cb\u7ec8\u201d\u56fa\u5b9a\u201c\u5728\u4e00\u6b21\u6e32\u67d3\u7684\u5404\u4e2a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u5185\u90e8\u3002<\/strong>\u4f60\u65e0\u9700\u62c5\u5fc3\u4ee3\u7801\u8fd0\u884c\u65f6 state \u662f\u5426\u53d1\u751f\u4e86\u53d8\u5316\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\uff0c\u4e07\u4e00\u4f60\u60f3\u5728\u91cd\u65b0\u6e32\u67d3\u4e4b\u524d\u8bfb\u53d6\u6700\u65b0\u7684 state \u600e\u4e48\u529e\uff1f\u4f60\u5e94\u8be5\u4f7f\u7528<a href=\"https:\/\/react.docschina.org\/learn\/queueing-a-series-of-state-updates\">\u72b6\u6001\u66f4\u65b0\u51fd\u6570<\/a>\uff0c\u4e0b\u4e00\u9875\u5c06\u4f1a\u4ecb\u7ecd\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6458\u8981<\/strong><\/h2>\n\n\n\n<ul>\n<li>\u8bbe\u7f6e state \u8bf7\u6c42\u4e00\u6b21\u65b0\u7684\u6e32\u67d3\u3002<\/li>\n\n\n\n<li>React \u5c06 state \u5b58\u50a8\u5728\u7ec4\u4ef6\u4e4b\u5916\uff0c\u5c31\u50cf\u5728\u67b6\u5b50\u4e0a\u4e00\u6837\u3002<\/li>\n\n\n\n<li>\u5f53\u4f60\u8c03\u7528\u00a0useState\u00a0\u65f6\uff0cReact \u4f1a\u4e3a\u4f60\u63d0\u4f9b<strong>\u8be5\u6b21\u6e32\u67d3<\/strong>\u00a0\u7684\u4e00\u5f20 state \u5feb\u7167\u3002<\/li>\n\n\n\n<li>\u53d8\u91cf\u548c\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e0d\u4f1a\u5728\u91cd\u6e32\u67d3\u4e2d\u201c\u5b58\u6d3b\u201d\u3002\u6bcf\u4e2a\u6e32\u67d3\u90fd\u6709\u81ea\u5df1\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u3002<\/li>\n\n\n\n<li>\u6bcf\u4e2a\u6e32\u67d3\uff08\u4ee5\u53ca\u5176\u4e2d\u7684\u51fd\u6570\uff09\u59cb\u7ec8\u201c\u770b\u5230\u201d\u7684\u662f React \u63d0\u4f9b\u7ed9<strong>\u8fd9\u4e2a<\/strong>\u00a0\u6e32\u67d3\u7684 state \u5feb\u7167\u3002<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u5728\u5fc3\u4e2d\u66ff\u6362\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u7684 state\uff0c\u7c7b\u4f3c\u4e8e\u66ff\u6362\u6e32\u67d3\u7684 JSX\u3002<\/li>\n\n\n\n<li>\u8fc7\u53bb\u521b\u5efa\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u62e5\u6709\u7684\u662f\u521b\u5efa\u5b83\u4eec\u7684\u90a3\u6b21\u6e32\u67d3\u4e2d\u7684 state \u503c\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>\u628a\u4e00\u7cfb\u5217 state \u66f4\u65b0\u52a0\u5165\u961f\u5217<\/strong><\/h1>\n\n\n\n<p>\u8bbe\u7f6e\u7ec4\u4ef6 state \u4f1a\u628a\u4e00\u6b21\u91cd\u65b0\u6e32\u67d3\u52a0\u5165\u961f\u5217\u3002\u4f46\u6709\u65f6\u4f60\u53ef\u80fd\u4f1a\u5e0c\u671b\u5728\u4e0b\u6b21\u6e32\u67d3\u52a0\u5165\u961f\u5217\u4e4b\u524d\u5bf9 state \u7684\u503c\u6267\u884c\u591a\u6b21\u64cd\u4f5c\u3002\u4e3a\u6b64\uff0c\u4e86\u89e3 React \u5982\u4f55\u6279\u91cf\u66f4\u65b0 state \u4f1a\u5f88\u6709\u5e2e\u52a9\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>React \u4f1a\u5bf9 state \u66f4\u65b0\u8fdb\u884c\u6279\u5904\u7406<\/strong><strong>&nbsp;<\/strong><\/h2>\n\n\n\n<p>\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u4f60\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u70b9\u51fb \u201c+3\u201d \u6309\u94ae\u4f1a\u4f7f\u8ba1\u6570\u5668\u9012\u589e\u4e09\u6b21\uff0c\u56e0\u4e3a\u5b83\u8c03\u7528\u4e86setNumber(number + 1)\u4e09\u6b21\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\n\nexport default function Counter() {\n  const &#91;number, setNumber] = useState(0);\n\n  return (\n    &lt;>\n      &lt;h1>{number}&lt;\/h1>\n      &lt;button onClick={() => {\n        setNumber(number + 1);\n        setNumber(number + 1);\n        setNumber(number + 1);\n      }}>+3&lt;\/button>\n    &lt;\/>\n  )\n}<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\uff0c\u4f60\u53ef\u80fd\u8fd8\u8bb0\u5f97\u4e0a\u4e00\u8282\u4e2d\u7684\u5185\u5bb9\uff0c<a href=\"https:\/\/react.docschina.org\/learn\/state-as-a-snapshot#rendering-takes-a-snapshot-in-time\">\u6bcf\u4e00\u6b21\u6e32\u67d3\u7684 state \u503c\u90fd\u662f\u56fa\u5b9a\u7684<\/a>\uff0c\u56e0\u6b64\u65e0\u8bba\u4f60\u8c03\u7528\u591a\u5c11\u6b21 setNumber(1)\uff0c\u5728\u7b2c\u4e00\u6b21\u6e32\u67d3\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u5185\u90e8\u7684 number \u503c\u603b\u662f 0 \uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>setNumber(0 + 1);\nsetNumber(0 + 1);\nsetNumber(0 + 1);<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u8fd9\u91cc\u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u5f71\u54cd\u56e0\u7d20\u9700\u8981\u8ba8\u8bba\u3002<strong>React \u4f1a\u7b49\u5230\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u7684<\/strong>\u6240\u6709<strong>\u4ee3\u7801\u90fd\u8fd0\u884c\u5b8c\u6bd5\u518d\u5904\u7406\u4f60\u7684 state \u66f4\u65b0\u3002<\/strong>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u91cd\u65b0\u6e32\u67d3\u53ea\u4f1a\u53d1\u751f\u5728\u6240\u6709\u8fd9\u4e9bsetNumber()\u8c03\u7528<strong>\u4e4b\u540e<\/strong>\u7684\u539f\u56e0\u3002<\/p>\n\n\n\n<p>\u8fd9\u53ef\u80fd\u4f1a\u8ba9\u4f60\u60f3\u8d77\u9910\u5385\u91cc\u5e2e\u4f60\u70b9\u83dc\u7684\u670d\u52a1\u5458\u3002\u670d\u52a1\u5458\u4e0d\u4f1a\u5728\u4f60\u8bf4\u7b2c\u4e00\u9053\u83dc\u7684\u65f6\u5019\u5c31\u8dd1\u5230\u53a8\u623f\uff01\u76f8\u53cd\uff0c\u4ed6\u4eec\u4f1a\u8ba9\u4f60\u628a\u83dc\u70b9\u5b8c\uff0c\u8ba9\u4f60\u4fee\u6539\u83dc\u54c1\uff0c\u751a\u81f3\u4f1a\u5e2e\u684c\u4e0a\u7684\u5176\u4ed6\u4eba\u70b9\u83dc\u3002<\/p>\n\n\n\n<p>\u8fd9\u8ba9\u4f60\u53ef\u4ee5\u66f4\u65b0\u591a\u4e2a state \u53d8\u91cf\u2014\u2014\u751a\u81f3\u6765\u81ea\u591a\u4e2a\u7ec4\u4ef6\u7684 state \u53d8\u91cf\u2014\u2014\u800c\u4e0d\u4f1a\u89e6\u53d1\u592a\u591a\u7684<a href=\"https:\/\/react.docschina.org\/learn\/render-and-commit#re-renders-when-state-updates\">\u91cd\u65b0\u6e32\u67d3<\/a>\u3002\u4f46\u8fd9\u4e5f\u610f\u5473\u7740\u53ea\u6709\u5728\u4f60\u7684\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u53ca\u5176\u4e2d\u4efb\u4f55\u4ee3\u7801\u6267\u884c\u5b8c\u6210<strong>\u4e4b\u540e<\/strong>\uff0cUI \u624d\u4f1a\u66f4\u65b0\u3002\u8fd9\u79cd\u7279\u6027\u4e5f\u5c31\u662f<strong>\u6279\u5904\u7406<\/strong>\uff0c\u5b83\u4f1a\u4f7f\u4f60\u7684 React \u5e94\u7528\u8fd0\u884c\u5f97\u66f4\u5feb\u3002\u5b83\u8fd8\u4f1a\u5e2e\u4f60\u907f\u514d\u5904\u7406\u53ea\u66f4\u65b0\u4e86\u4e00\u90e8\u5206 state \u53d8\u91cf\u7684\u4ee4\u4eba\u56f0\u60d1\u7684\u201c\u534a\u6210\u54c1\u201d\u6e32\u67d3\u3002<\/p>\n\n\n\n<p><strong>React \u4e0d\u4f1a\u8de8<\/strong><strong><em>\u591a\u4e2a<\/em><\/strong><strong>\u9700\u8981\u523b\u610f\u89e6\u53d1\u7684\u4e8b\u4ef6\uff08\u5982\u70b9\u51fb\uff09\u8fdb\u884c\u6279\u5904\u7406<\/strong>\u2014\u2014\u6bcf\u6b21\u70b9\u51fb\u90fd\u662f\u5355\u72ec\u5904\u7406\u7684\u3002\u8bf7\u653e\u5fc3\uff0cReact \u53ea\u4f1a\u5728\u4e00\u822c\u6765\u8bf4\u5b89\u5168\u7684\u60c5\u51b5\u4e0b\u624d\u8fdb\u884c\u6279\u5904\u7406\u3002\u8fd9\u53ef\u4ee5\u786e\u4fdd\uff0c\u4f8b\u5982\uff0c\u5982\u679c\u7b2c\u4e00\u6b21\u70b9\u51fb\u6309\u94ae\u4f1a\u7981\u7528\u8868\u5355\uff0c\u90a3\u4e48\u7b2c\u4e8c\u6b21\u70b9\u51fb\u5c31\u4e0d\u4f1a\u518d\u6b21\u63d0\u4ea4\u5b83\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u5728\u4e0b\u6b21\u6e32\u67d3\u524d\u591a\u6b21\u66f4\u65b0\u540c\u4e00\u4e2a state<\/strong><strong>&nbsp;<\/strong><\/h2>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u4e0d\u5e38\u89c1\u7684\u7528\u4f8b\uff0c\u4f46\u662f\u5982\u679c\u4f60\u60f3\u5728\u4e0b\u6b21\u6e32\u67d3\u4e4b\u524d\u591a\u6b21\u66f4\u65b0\u540c\u4e00\u4e2a state\uff0c\u4f60\u53ef\u4ee5\u50cfsetNumber(n =&gt; n + 1)\u8fd9\u6837\u4f20\u5165\u4e00\u4e2a\u6839\u636e\u961f\u5217\u4e2d\u7684\u524d\u4e00\u4e2a state \u8ba1\u7b97\u4e0b\u4e00\u4e2a state \u7684<strong>\u51fd\u6570<\/strong>\uff0c\u800c\u4e0d\u662f\u50cfsetNumber(number + 1)\u8fd9\u6837\u4f20\u5165<strong>\u4e0b\u4e00\u4e2a state \u503c<\/strong>\u3002\u8fd9\u662f\u4e00\u79cd\u544a\u8bc9 React \u201c\u7528 state \u503c\u505a\u67d0\u4e8b\u201d\u800c\u4e0d\u662f\u4ec5\u4ec5\u66ff\u6362\u5b83\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u5c1d\u8bd5\u9012\u589e\u8ba1\u6570\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\n\nexport default function Counter() {\n  const &#91;number, setNumber] = useState(0);\n\n  return (\n    &lt;>\n      &lt;h1>{number}&lt;\/h1>\n      &lt;button onClick={() => {\n        setNumber(n => n + 1);\n        setNumber(n => n + 1);\n        setNumber(n => n + 1);\n      }}>+3&lt;\/button>\n    &lt;\/>\n  )\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff0cn =&gt; n + 1\u88ab\u79f0\u4e3a<strong>\u66f4\u65b0\u51fd\u6570<\/strong>\u3002\u5f53\u4f60\u5c06\u5b83\u4f20\u9012\u7ed9\u4e00\u4e2a state \u8bbe\u7f6e\u51fd\u6570\u65f6\uff1a<\/p>\n\n\n\n<ol>\n<li>React \u4f1a\u5c06\u6b64\u51fd\u6570\u52a0\u5165\u961f\u5217\uff0c\u4ee5\u4fbf\u5728\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4e2d\u7684\u6240\u6709\u5176\u4ed6\u4ee3\u7801\u8fd0\u884c\u540e\u8fdb\u884c\u5904\u7406\u3002<\/li>\n\n\n\n<li>\u5728\u4e0b\u4e00\u6b21\u6e32\u67d3\u671f\u95f4\uff0cReact \u4f1a\u904d\u5386\u961f\u5217\u5e76\u7ed9\u4f60\u66f4\u65b0\u4e4b\u540e\u7684\u6700\u7ec8 state\u3002<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>setNumber(n => n + 1);\nsetNumber(n => n + 1);\nsetNumber(n => n + 1);<\/code><\/pre>\n\n\n\n<p>\u4e0b\u9762\u662f React \u5728\u6267\u884c\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u65f6\u5904\u7406\u8fd9\u51e0\u884c\u4ee3\u7801\u7684\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<ol>\n<li>setNumber(n => n + 1)\uff1an => n + 1\u00a0\u662f\u4e00\u4e2a\u51fd\u6570\u3002React \u5c06\u5b83\u52a0\u5165\u961f\u5217\u3002<\/li>\n\n\n\n<li>setNumber(n => n + 1)\uff1an => n + 1\u00a0\u662f\u4e00\u4e2a\u51fd\u6570\u3002React \u5c06\u5b83\u52a0\u5165\u961f\u5217\u3002<\/li>\n\n\n\n<li>setNumber(n => n + 1)\uff1an => n + 1\u00a0\u662f\u4e00\u4e2a\u51fd\u6570\u3002React \u5c06\u5b83\u52a0\u5165\u961f\u5217\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u5f53\u4f60\u5728\u4e0b\u6b21\u6e32\u67d3\u671f\u95f4\u8c03\u7528useState\u65f6\uff0cReact \u4f1a\u904d\u5386\u961f\u5217\u3002\u4e4b\u524d\u7684numberstate \u7684\u503c\u662f0\uff0c\u6240\u4ee5\u8fd9\u5c31\u662f React \u4f5c\u4e3a\u53c2\u6570n\u4f20\u9012\u7ed9\u7b2c\u4e00\u4e2a\u66f4\u65b0\u51fd\u6570\u7684\u503c\u3002\u7136\u540e React \u4f1a\u83b7\u53d6\u4f60\u4e0a\u4e00\u4e2a\u66f4\u65b0\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3an\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u66f4\u65b0\u51fd\u6570\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-70.png\"><img loading=\"lazy\" decoding=\"async\" width=\"297\" height=\"171\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-70.png\" alt=\"\" class=\"wp-image-1280\"\/><\/a><\/figure>\n\n\n\n<p>React \u4f1a\u4fdd\u5b583\u4e3a\u6700\u7ec8\u7ed3\u679c\u5e76\u4eceuseState\u4e2d\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\u70b9\u51fb\u201c+3\u201d\u6b63\u786e\u5730\u5c06\u503c\u589e\u52a0\u201c+3\u201d\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5982\u679c\u4f60\u5728\u66ff\u6362 state \u540e\u66f4\u65b0 state \u4f1a\u53d1\u751f\u4ec0\u4e48<\/strong><strong>&nbsp;<\/strong><\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u4f1a\u600e\u4e48\u6837\uff1f\u4f60\u8ba4\u4e3anumber\u5728\u4e0b\u4e00\u6b21\u6e32\u67d3\u4e2d\u7684\u503c\u662f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button onClick={() => {\n    setNumber(number + 5);\n    setNumber(n => n + 1);\n}}><\/code><\/pre>\n\n\n\n<p>\u518d\u770b\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\n\nexport default function Counter() {\n  const &#91;number, setNumber] = useState(0);\n\n  return (\n    &lt;>\n      &lt;h1>{number}&lt;\/h1>\n      &lt;button onClick={() => {\n        setNumber(number + 5);\n        setNumber(n => n + 1);\n      }}>\u589e\u52a0\u6570\u5b57&lt;\/button>\n    &lt;\/>\n  )\n}<\/code><\/pre>\n\n\n\n<p>\u8fd9\u662f\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u544a\u8bc9 React \u8981\u505a\u7684\u4e8b\u60c5\uff1a<\/p>\n\n\n\n<ol>\n<li>setNumber(number + 5)\uff1anumber\u00a0\u4e3a\u00a00\uff0c\u6240\u4ee5\u00a0setNumber(0 + 5)\u3002React \u5c06\u00a0<em>\u201c\u66ff\u6362\u4e3a<\/em><em>\u00a0<\/em><em>5<\/em><em>\u201d<\/em>\u00a0\u6dfb\u52a0\u5230\u5176\u961f\u5217\u4e2d\u3002<\/li>\n\n\n\n<li>setNumber(n => n + 1)\uff1an => n + 1\u00a0\u662f\u4e00\u4e2a\u66f4\u65b0\u51fd\u6570\u3002 React \u5c06\u00a0<strong>\u8be5\u51fd\u6570<\/strong>\u00a0\u6dfb\u52a0\u5230\u5176\u961f\u5217\u4e2d\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u5728\u4e0b\u4e00\u6b21\u6e32\u67d3\u671f\u95f4\uff0cReact \u4f1a\u904d\u5386 state \u961f\u5217\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-71.png\"><img loading=\"lazy\" decoding=\"async\" width=\"386\" height=\"121\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-71.png\" alt=\"\" class=\"wp-image-1281\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-71.png 386w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-71-300x94.png 300w\" sizes=\"(max-width: 386px) 100vw, 386px\" \/><\/a><\/figure>\n\n\n\n<p>React \u4f1a\u4fdd\u5b58 6 \u4e3a\u6700\u7ec8\u7ed3\u679c\u5e76\u4ece useState \u4e2d\u8fd4\u56de\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u6ce8\u610f<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\uff0csetState(x)\u5b9e\u9645\u4e0a\u4f1a\u50cfsetState(n =&gt; x)\u4e00\u6837\u8fd0\u884c\uff0c\u53ea\u662f\u6ca1\u6709\u4f7f\u7528n\uff01<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u5982\u679c\u4f60\u5728\u66f4\u65b0 state \u540e\u66ff\u6362 state \u4f1a\u53d1\u751f\u4ec0\u4e48<\/strong><strong>&nbsp;<\/strong><\/h3>\n\n\n\n<p>\u8ba9\u6211\u4eec\u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\u3002\u4f60\u8ba4\u4e3anumber\u5728\u4e0b\u4e00\u6b21\u6e32\u67d3\u4e2d\u7684\u503c\u662f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;button onClick={() => {\n    setNumber(number + 5);\n    setNumber(n => n + 1);\n    setNumber(42);\n}}><\/code><\/pre>\n\n\n\n<p>\u770b\u4e0b\u9762\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\n\nexport default function Counter() {\n  const &#91;number, setNumber] = useState(0);\n\n  return (\n    &lt;>\n      &lt;h1>{number}&lt;\/h1>\n      &lt;button onClick={() => {\n        setNumber(number + 5);\n        setNumber(n => n + 1);\n        setNumber(42);\n      }}>\u589e\u52a0\u6570\u5b57&lt;\/button>\n    &lt;\/>\n  )\n}<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0b\u662f React \u5728\u6267\u884c\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u65f6\u5904\u7406\u8fd9\u51e0\u884c\u4ee3\u7801\u7684\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<ol>\n<li>setNumber(number + 5)\uff1anumber\u00a0\u4e3a\u00a00\uff0c\u6240\u4ee5\u00a0setNumber(0 + 5)\u3002React \u5c06\u00a0<em>\u201c\u66ff\u6362\u4e3a<\/em><em>\u00a0<\/em><em>5<\/em><em>\u201d<\/em>\u00a0\u6dfb\u52a0\u5230\u5176\u961f\u5217\u4e2d\u3002<\/li>\n\n\n\n<li>setNumber(n => n + 1)\uff1an => n + 1\u00a0\u662f\u4e00\u4e2a\u66f4\u65b0\u51fd\u6570\u3002React \u5c06\u8be5\u51fd\u6570\u6dfb\u52a0\u5230\u5176\u961f\u5217\u4e2d\u3002<\/li>\n\n\n\n<li>setNumber(42)\uff1aReact \u5c06\u00a0<em>\u201c\u66ff\u6362\u4e3a<\/em><em>\u00a0<\/em><em>42<\/em><em>\u201d<\/em>\u00a0\u6dfb\u52a0\u5230\u5176\u961f\u5217\u4e2d\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u5728\u4e0b\u4e00\u6b21\u6e32\u67d3\u671f\u95f4\uff0cReact \u4f1a\u904d\u5386 state \u961f\u5217\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-72.png\"><img loading=\"lazy\" decoding=\"async\" width=\"421\" height=\"171\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-72.png\" alt=\"\" class=\"wp-image-1282\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-72.png 421w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-72-300x122.png 300w\" sizes=\"(max-width: 421px) 100vw, 421px\" \/><\/a><\/figure>\n\n\n\n<p>\u7136\u540e React \u4f1a\u4fdd\u5b5842\u4e3a\u6700\u7ec8\u7ed3\u679c\u5e76\u4eceuseState\u4e2d\u8fd4\u56de\u3002<\/p>\n\n\n\n<p>\u603b\u800c\u8a00\u4e4b\uff0c\u4ee5\u4e0b\u662f\u4f60\u53ef\u4ee5\u8003\u8651\u4f20\u9012\u7ed9setNumberstate \u8bbe\u7f6e\u51fd\u6570\u7684\u5185\u5bb9\uff1a<\/p>\n\n\n\n<ul>\n<li><strong>\u4e00\u4e2a\u66f4\u65b0\u51fd\u6570<\/strong>\uff08\u4f8b\u5982\uff1an => n + 1\uff09\u4f1a\u88ab\u6dfb\u52a0\u5230\u961f\u5217\u4e2d\u3002<\/li>\n\n\n\n<li><strong>\u4efb\u4f55\u5176\u4ed6\u7684\u503c<\/strong>\uff08\u4f8b\u5982\uff1a\u6570\u5b57\u00a05\uff09\u4f1a\u5bfc\u81f4\u201c\u66ff\u6362\u4e3a\u00a05\u201d\u88ab\u6dfb\u52a0\u5230\u961f\u5217\u4e2d\uff0c\u5df2\u7ecf\u5728\u961f\u5217\u4e2d\u7684\u5185\u5bb9\u4f1a\u88ab\u5ffd\u7565\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u6267\u884c\u5b8c\u6210\u540e\uff0cReact \u5c06\u89e6\u53d1\u91cd\u65b0\u6e32\u67d3\u3002\u5728\u91cd\u65b0\u6e32\u67d3\u671f\u95f4\uff0cReact \u5c06\u5904\u7406\u961f\u5217\u3002\u66f4\u65b0\u51fd\u6570\u4f1a\u5728\u6e32\u67d3\u671f\u95f4\u6267\u884c\uff0c\u56e0\u6b64<strong>\u66f4\u65b0\u51fd\u6570\u5fc5\u987b\u662f<\/strong><a href=\"https:\/\/react.docschina.org\/learn\/keeping-components-pure\">\u7eaf\u51fd\u6570<\/a>\u5e76\u4e14\u53ea<strong>\u8fd4\u56de<\/strong>\u7ed3\u679c\u3002\u4e0d\u8981\u5c1d\u8bd5\u4ece\u5b83\u4eec\u5185\u90e8\u8bbe\u7f6e state \u6216\u8005\u6267\u884c\u5176\u4ed6\u526f\u4f5c\u7528\u3002\u5728\u4e25\u683c\u6a21\u5f0f\u4e0b\uff0cReact \u4f1a\u6267\u884c\u6bcf\u4e2a\u66f4\u65b0\u51fd\u6570\u4e24\u6b21\uff08\u4f46\u662f\u4e22\u5f03\u7b2c\u4e8c\u4e2a\u7ed3\u679c\uff09\u4ee5\u4fbf\u5e2e\u52a9\u4f60\u53d1\u73b0\u9519\u8bef\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u6458\u8981<\/strong><\/h2>\n\n\n\n<ul>\n<li>\u8bbe\u7f6e state \u4e0d\u4f1a\u66f4\u6539\u73b0\u6709\u6e32\u67d3\u4e2d\u7684\u53d8\u91cf\uff0c\u4f46\u4f1a\u8bf7\u6c42\u4e00\u6b21\u65b0\u7684\u6e32\u67d3\u3002<\/li>\n\n\n\n<li>React \u4f1a\u5728\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\u6267\u884c\u5b8c\u6210\u4e4b\u540e\u5904\u7406 state \u66f4\u65b0\u3002\u8fd9\u88ab\u79f0\u4e3a\u6279\u5904\u7406\u3002<\/li>\n\n\n\n<li>\u8981\u5728\u4e00\u4e2a\u4e8b\u4ef6\u4e2d\u591a\u6b21\u66f4\u65b0\u67d0\u4e9b state\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u00a0setNumber(n => n + 1)\u00a0\u66f4\u65b0\u51fd\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u76f4\u63a5\u4fee\u6539React\u7684State<\/h1>\n\n\n\n<p>\u53c2\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/juejin.cn\/post\/7033590099992379422\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/juejin.cn\/post\/7033590099992379422<\/a><br><a href=\"https:\/\/www.zhihu.com\/question\/440916294\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.zhihu.com\/question\/440916294<\/a><br><a href=\"https:\/\/cloud.tencent.com\/developer\/article\/2001534\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/cloud.tencent.com\/developer\/article\/2001534<\/a><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48\u591a\u4e2a JSX \u6807\u7b7e\u9700\u8981\u88ab\u4e00\u4e2a\u7236\u5143\u7d20\u5305\u88f9\uff1f JSX \u867d\u7136\u770b\u8d77\u6765\u5f88\u50cf HTML\uff0c\u4f46\u5728\u5e95\u5c42\u5176\u5b9e\u88ab\u8f6c\u5316\u4e3a\u4e86 Ja&#8230;<\/p>\n","protected":false},"author":1,"featured_media":1019,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[56],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>React \u77e5\u8bc6\u70b9\u603b\u7ed3 - \u7801\u5148\u751f\u7684\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=\"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React \u77e5\u8bc6\u70b9\u603b\u7ed3 - \u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"\u4e3a\u4ec0\u4e48\u591a\u4e2a JSX \u6807\u7b7e\u9700\u8981\u88ab\u4e00\u4e2a\u7236\u5143\u7d20\u5305\u88f9\uff1f JSX \u867d\u7136\u770b\u8d77\u6765\u5f88\u50cf HTML\uff0c\u4f46\u5728\u5e95\u5c42\u5176\u5b9e\u88ab\u8f6c\u5316\u4e3a\u4e86 Ja...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-28T16:59:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-08-03T17:55:42+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/07\/react-logo.png\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"\u7801\u5148\u751f\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/mrcode2021\" \/>\n<meta name=\"twitter:site\" content=\"@mrcode2021\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u7801\u5148\u751f\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/\"},\"author\":{\"name\":\"\u7801\u5148\u751f\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"headline\":\"React \u77e5\u8bc6\u70b9\u603b\u7ed3\",\"datePublished\":\"2024-07-28T16:59:18+00:00\",\"dateModified\":\"2024-08-03T17:55:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/\"},\"wordCount\":377,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"keywords\":[\"react\"],\"articleSection\":[\"\u524d\u7aef\u6280\u672f\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/\",\"url\":\"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/\",\"name\":\"React \u77e5\u8bc6\u70b9\u603b\u7ed3 - \u7801\u5148\u751f\u7684\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/#website\"},\"datePublished\":\"2024-07-28T16:59:18+00:00\",\"dateModified\":\"2024-08-03T17:55:42+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/codermr.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React \u77e5\u8bc6\u70b9\u603b\u7ed3\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/codermr.com\/#website\",\"url\":\"http:\/\/codermr.com\/\",\"name\":\"\u7801\u5148\u751f\u7684\u535a\u5ba2\",\"description\":\"\u6b22 \u8fce \u4e0b \u8f7d \u6211 \u5f00 \u53d1 \u7684 \u5404 \u7aef \u8f6f \u4ef6 \u548c APP\",\"publisher\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/codermr.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\",\"name\":\"\u7801\u5148\u751f\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg\",\"contentUrl\":\"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg\",\"width\":400,\"height\":400,\"caption\":\"\u7801\u5148\u751f\"},\"logo\":{\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/codermr.com\",\"https:\/\/twitter.com\/https:\/\/twitter.com\/mrcode2021\"],\"url\":\"http:\/\/codermr.com\/index.php\/author\/coderma\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"React \u77e5\u8bc6\u70b9\u603b\u7ed3 - \u7801\u5148\u751f\u7684\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":"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/","og_locale":"zh_CN","og_type":"article","og_title":"React \u77e5\u8bc6\u70b9\u603b\u7ed3 - \u7801\u5148\u751f\u7684\u535a\u5ba2","og_description":"\u4e3a\u4ec0\u4e48\u591a\u4e2a JSX \u6807\u7b7e\u9700\u8981\u88ab\u4e00\u4e2a\u7236\u5143\u7d20\u5305\u88f9\uff1f JSX \u867d\u7136\u770b\u8d77\u6765\u5f88\u50cf HTML\uff0c\u4f46\u5728\u5e95\u5c42\u5176\u5b9e\u88ab\u8f6c\u5316\u4e3a\u4e86 Ja...","og_url":"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/","og_site_name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","article_published_time":"2024-07-28T16:59:18+00:00","article_modified_time":"2024-08-03T17:55:42+00:00","og_image":[{"width":900,"height":675,"url":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/07\/react-logo.png","type":"image\/png"}],"author":"\u7801\u5148\u751f","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/mrcode2021","twitter_site":"@mrcode2021","twitter_misc":{"\u4f5c\u8005":"\u7801\u5148\u751f","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/#article","isPartOf":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/"},"author":{"name":"\u7801\u5148\u751f","@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"headline":"React \u77e5\u8bc6\u70b9\u603b\u7ed3","datePublished":"2024-07-28T16:59:18+00:00","dateModified":"2024-08-03T17:55:42+00:00","mainEntityOfPage":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/"},"wordCount":377,"commentCount":0,"publisher":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"keywords":["react"],"articleSection":["\u524d\u7aef\u6280\u672f"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/","url":"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/","name":"React \u77e5\u8bc6\u70b9\u603b\u7ed3 - \u7801\u5148\u751f\u7684\u535a\u5ba2","isPartOf":{"@id":"http:\/\/codermr.com\/#website"},"datePublished":"2024-07-28T16:59:18+00:00","dateModified":"2024-08-03T17:55:42+00:00","breadcrumb":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/codermr.com\/index.php\/2024\/07\/29\/react_note_summary1\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/codermr.com\/"},{"@type":"ListItem","position":2,"name":"React \u77e5\u8bc6\u70b9\u603b\u7ed3"}]},{"@type":"WebSite","@id":"http:\/\/codermr.com\/#website","url":"http:\/\/codermr.com\/","name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","description":"\u6b22 \u8fce \u4e0b \u8f7d \u6211 \u5f00 \u53d1 \u7684 \u5404 \u7aef \u8f6f \u4ef6 \u548c APP","publisher":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/codermr.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf","name":"\u7801\u5148\u751f","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"http:\/\/codermr.com\/#\/schema\/person\/image\/","url":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg","contentUrl":"http:\/\/codermr.com\/wp-content\/uploads\/2023\/02\/wukong.jpg","width":400,"height":400,"caption":"\u7801\u5148\u751f"},"logo":{"@id":"http:\/\/codermr.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/codermr.com","https:\/\/twitter.com\/https:\/\/twitter.com\/mrcode2021"],"url":"http:\/\/codermr.com\/index.php\/author\/coderma\/"}]}},"_links":{"self":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1278"}],"collection":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/comments?post=1278"}],"version-history":[{"count":2,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1278\/revisions"}],"predecessor-version":[{"id":1284,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1278\/revisions\/1284"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/media\/1019"}],"wp:attachment":[{"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/media?parent=1278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/categories?post=1278"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/tags?post=1278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}