{"id":1241,"date":"2024-07-28T15:08:18","date_gmt":"2024-07-28T07:08:18","guid":{"rendered":"http:\/\/codermr.com\/?p=1241"},"modified":"2024-07-28T15:08:19","modified_gmt":"2024-07-28T07:08:19","slug":"state-a-components-memory","status":"publish","type":"post","link":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/","title":{"rendered":"State: A Component&#8217;s Memory"},"content":{"rendered":"\n<p><code>State<\/code> \u5c31\u662f\u7ec4\u4ef6\u7684\u8bb0\u5fc6\u3002<\/p>\n\n\n\n<p>Components often need to change what\u2019s on the screen <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">as(\u6839\u636e)<\/mark><\/strong> a result of an interaction. <br>Typing into the form should update the input field\uff08\u8f93\u5165\u8868\u5355\u5e94\u8be5\u66f4\u65b0\u8f93\u5165\u5b57\u6bb5\uff09, clicking \u201cnext\u201d on an image <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">carousel <\/mark>should change which image is displayed, clicking \u201cbuy\u201d should put a product in the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">shopping cart<\/mark>\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u8d2d\u7269\u8f66<\/mark>\uff09. <br>Components need to \u201cremember\u201d things\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u7ec4\u4ef6\u9700\u8981\u8bb0\u4f4f\u4e00\u4e9b\u4e1c\u897f<\/mark>\uff09: the current input value, the current image, the shopping cart. In React, this kind of component-specific memory is called <em>state<\/em>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-67.png\"><img loading=\"lazy\" decoding=\"async\" width=\"736\" height=\"131\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-67.png\" alt=\"\" class=\"wp-image-1245\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-67.png 736w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-67-300x53.png 300w\" sizes=\"(max-width: 736px) 100vw, 736px\" \/><\/a><\/figure>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><em><strong>carousel <\/strong>\/&#8217;k\u00e6r\u0259&#8217;s\u025bl\/<\/em><\/mark><em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"> \u8f6e\u64ad,\u65cb\u8f6c<\/mark><\/em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><em>\uff1b<strong>sculpture <\/strong>\/\u02c8sk\u028clpt\u0283\u0259r\/ \u96d5\u523b, \u96d5\u5851, \u96d5\u523b\u54c1<\/em>\uff1b<\/mark> <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">You will learn<\/h3>\n\n\n\n<ul>\n<li>How to add a state variable with the\u00a0<a href=\"https:\/\/react.dev\/reference\/react\/useState\"><code>useState<\/code><\/a>\u00a0Hook\uff08\u5982\u4f55\u4f7f\u7528\u00a0<a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/useState\"><code>useState<\/code><\/a>\u00a0Hook \u6dfb\u52a0 state \u53d8\u91cf\uff09<\/li>\n\n\n\n<li>What <strong><em>pair of values<\/em><\/strong> the\u00a0<code>useState<\/code>\u00a0Hook returns\uff08<code>useState<\/code>\u00a0Hook \u8fd4\u56de\u54ea\u4e00\u5bf9\u503c\uff09<\/li>\n\n\n\n<li>How to add more than one state variable<\/li>\n\n\n\n<li>Why state is called local\uff08\u4e3a\u4ec0\u4e48 state \u88ab\u79f0\u4f5c\u662f\u5c40\u90e8\u7684\uff09<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"when-a-regular-variable-isnt-enough\">When a regular variable isn\u2019t enough\u00a0<a href=\"https:\/\/react.dev\/learn\/state-a-components-memory#when-a-regular-variable-isnt-enough\"><\/a>\uff08\u5f53\u666e\u901a\u7684\u53d8\u91cf\u65e0\u6cd5\u6ee1\u8db3\u65f6\uff09<\/h2>\n\n\n\n<p>Here\u2019s a component that renders a <strong><em>sculpture <\/em><\/strong>image. Clicking the \u201cNext\u201d button should show the next sculpture by changing the <code>index<\/code> to <code>1<\/code>, then <code>2<\/code>, and so on. However, this <strong>won\u2019t work<\/strong> (you can try it!)\u3010<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u70b9\u51fb \u201cNext\u201d \u6309\u94ae\u5e94\u8be5\u663e\u793a\u4e0b\u4e00\u4e2a\u96d5\u5851\u5e76\u5c06 <code>index<\/code> \u66f4\u6539\u4e3a <code>1<\/code>\uff0c\u518d\u6b21\u70b9\u51fb\u53c8\u66f4\u6539\u4e3a <code>2<\/code>\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\u4f46\u8fd9\u4e2a\u7ec4\u4ef6\u73b0\u5728<strong>\u4e0d\u8d77\u4f5c\u7528<\/strong>\uff08\u4f60\u53ef\u4ee5\u8bd5\u4e00\u8bd5\uff01\uff09<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><strong>==\u300b\u4e3a\u5565\u4e0d\u8d77\u4f5c\u7528\u5462\uff1f\u56e0\u4e3a\u4f60\u6bcf\u6b21\u70b9\u51fb\u6309\u94ae\uff0c\u5c31\u4f1a\u91cd\u65b0\u6e32\u67d3\u9875\u9762\uff0c\u800cindex\u662f\u4e2a\u5c40\u90e8\u53d8\u91cf<\/strong><\/mark>\u3002\u3011:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { sculptureList } from '.\/data.js';\n\nexport default function Gallery() {\n  let index = 0;\n\n  function handleClick() {\n    index = index + 1;\/\/ \u6bcf\u6b21\u70b9\u51fb\uff0c\u5c06index\u52a01\n  }\n\n  let sculpture = sculptureList&#91;index];\n  return (\n    &lt;>\n      &lt;button onClick={handleClick}>\n        Next\uff08\u663e\u793a\u4e0b\u4e00\u5f20\uff09\n      &lt;\/button>\n      &lt;h2>\n        &lt;i>{sculpture.name} &lt;\/i> \n        by {sculpture.artist}\n      &lt;\/h2>\n      &lt;h3>  \n        ({index + 1} of {sculptureList.length})\n      &lt;\/h3>\n      &lt;img \n        src={sculpture.url} \n        alt={sculpture.alt}\n      \/>\n      &lt;p>\n        {sculpture.description}\n      &lt;\/p>\n    &lt;\/>\n  );\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-65.png\"><img loading=\"lazy\" decoding=\"async\" width=\"507\" height=\"374\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-65.png\" alt=\"\" class=\"wp-image-1242\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-65.png 507w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-65-300x221.png 300w\" sizes=\"(max-width: 507px) 100vw, 507px\" \/><\/a><\/figure>\n\n\n\n<p>The <code>handleClick<\/code> event handler is updating a local variable, <code>index<\/code>. But two things prevent that change from being visible:<\/p>\n\n\n\n<ol>\n<li><strong>Local variables don\u2019t <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">persist<\/mark> between renders.<\/strong>\u00a0When React renders this component <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><strong><em>a second time\uff08\u7b2c\u4e8c\u6b21\uff09<\/em><\/strong><\/mark>, <strong><em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">it renders it from scratch\uff08\u5b83\u4ece\u5934\u5f00\u59cb\u6e32\u67d3\uff09<\/mark><\/em><\/strong>\u2014it doesn\u2019t consider any changes to the local variables.<\/li>\n\n\n\n<li><strong>Changes to local variables won\u2019t trigger renders\uff08<strong>\u66f4\u6539\u5c40\u90e8\u53d8\u91cf\u4e0d\u4f1a\u89e6\u53d1\u6e32\u67d3<\/strong>\uff09.<\/strong>\u00a0React doesn\u2019t realize it needs to render the component again with the new data.<\/li>\n<\/ol>\n\n\n\n<p>To update a component with new data, two things need to happen:<\/p>\n\n\n\n<ol>\n<li><strong>Retain<\/strong>\u00a0the data between renders.\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u4fdd\u7559<\/strong>\u00a0\u6e32\u67d3\u4e4b\u95f4\u7684\u6570\u636e<\/mark>\uff09<\/li>\n\n\n\n<li><strong>Trigger<\/strong>\u00a0React to render the component with new data (re-rendering).<\/li>\n<\/ol>\n\n\n\n<p>The <a href=\"https:\/\/react.dev\/reference\/react\/useState\"><code>useState<\/code><\/a> Hook provides those two things:<\/p>\n\n\n\n<ol>\n<li>A\u00a0<strong>state variable<\/strong>\u00a0to retain the data between renders.<\/li>\n\n\n\n<li>A\u00a0<strong>state setter function<\/strong>\u00a0to update the variable and trigger React to render the component again.\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>State setter \u51fd\u6570<\/strong>\u00a0\u66f4\u65b0\u53d8\u91cf\u5e76\u89e6\u53d1 React \u518d\u6b21\u6e32\u67d3\u7ec4\u4ef6\u3002<\/mark>\uff09<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"adding-a-state-variable\">Adding a state variable&nbsp;<a href=\"https:\/\/react.dev\/learn\/state-a-components-memory#adding-a-state-variable\"><\/a><\/h2>\n\n\n\n<p>To add a state variable, import <code>useState<\/code> from React at the top of the file\uff08\u8981\u6dfb\u52a0 state \u53d8\u91cf\uff0c\u5148\u4ece\u6587\u4ef6\u9876\u90e8\u7684 React \u4e2d\u5bfc\u5165 <code>useState<\/code>\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';<\/code><\/pre>\n\n\n\n<p><code>index<\/code> is a state variable and <code>setIndex<\/code> is the setter function.<\/p>\n\n\n\n<p><strong><code>index<\/code> \u662f\u4e00\u4e2a state \u53d8\u91cf\uff0c<code>setIndex<\/code> \u662f\u5bf9\u5e94\u7684 setter \u51fd\u6570\u3002<\/strong>\u2588\u2588\u2588\u2588\u2588\u2588<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>The <code>[<\/code> and <code>]<\/code> syntax here is called <a href=\"https:\/\/javascript.info\/destructuring-assignment\" target=\"_blank\" rel=\"noreferrer noopener\">array destructuring<\/a> and it lets you read values from an array. The array returned by <code>useState<\/code> always has exactly two items.\uff08<strong>\u8fd9\u91cc\u7684 <code>[<\/code> \u548c <code>]<\/code> \u8bed\u6cd5\u79f0\u4e3a<a href=\"https:\/\/zh.javascript.info\/destructuring-assignment\" target=\"_blank\" rel=\"noreferrer noopener\">\u6570\u7ec4\u89e3\u6784<\/a>\uff0c\u5b83\u5141\u8bb8\u4f60\u4ece\u6570\u7ec4\u4e2d\u8bfb\u53d6\u503c\u3002 <code>useState<\/code> \u8fd4\u56de\u7684\u6570\u7ec4\u603b\u662f\u6b63\u597d\u6709\u4e24\u9879\u3002<\/strong>\uff09<\/p>\n<\/blockquote>\n\n\n\n<p>Now clicking the \u201cNext\u201d button switches the current sculpture:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\nimport { sculptureList } from '.\/data.js';\n\nexport default function Gallery() {\n  const &#91;index, setIndex] = useState(0);\/\/\u2588\u2588\u5c06index\u7684\u503c\u653e\u8fdbstate\u4e2d\u4e86\uff01\n\n  function handleClick() {\n    setIndex(index + 1);\n  }\n\n  let sculpture = sculptureList&#91;index];\n  return (\n    &lt;>\n      &lt;button onClick={handleClick}>\n        Next\n      &lt;\/button>\n      &lt;h2>\n        &lt;i>{sculpture.name} &lt;\/i> \n        by {sculpture.artist}\n      &lt;\/h2>\n      &lt;h3>  \n        ({index + 1} of {sculptureList.length})\n      &lt;\/h3>\n      &lt;img \n        src={sculpture.url} \n        alt={sculpture.alt}\n      \/>\n      &lt;p>\n        {sculpture.description}\n      &lt;\/p>\n    &lt;\/>\n  );\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"meet-your-first-hook\">Meet your first Hook&nbsp;<a href=\"https:\/\/react.dev\/learn\/state-a-components-memory#meet-your-first-hook\"><\/a><\/h3>\n\n\n\n<p style=\"font-size:18px\">In React, <code>useState<\/code>, as well as any other function starting with \u201c<code>use<\/code>\u201d, is called a Hook.\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5728 React \u4e2d\uff0c<code>useState<\/code> \u4ee5\u53ca\u4efb\u4f55\u5176\u4ed6\u4ee5\u201c<code>use<\/code>\u201d\u5f00\u5934\u7684\u51fd\u6570\u90fd\u88ab\u79f0\u4e3a Hook\u3002<\/mark><\/strong>\uff09<\/p>\n\n\n\n<p><em>Hooks<\/em> are special functions that are only available while React is <a href=\"https:\/\/react.dev\/learn\/render-and-commit#step-1-trigger-a-render\">rendering<\/a> (which we\u2019ll get into in more detail on the next page). They let you \u201chook into\u201d different React features.\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Hook \u662f\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u53ea\u5728 React <a href=\"https:\/\/zh-hans.react.dev\/learn\/render-and-commit#step-1-trigger-a-render\">\u6e32\u67d3<\/a>\u65f6\u6709\u6548\u3002\u5b83\u4eec\u80fd\u8ba9\u4f60 \u201chook\u201d \u5230\u4e0d\u540c\u7684 React \u7279\u6027\u4e2d\u53bb\u3002<\/mark><\/strong>\uff09<\/p>\n\n\n\n<p>State is just one of those features, but you will meet the other Hooks later.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Pitfall<\/h3>\n\n\n\n<p><strong>Hooks\u2014functions starting with <code>use<\/code>\u2014can only be called at the top level of your components or <a href=\"https:\/\/react.dev\/learn\/reusing-logic-with-custom-hooks\">your own Hooks.<\/a><\/strong> You can\u2019t call Hooks inside conditions, loops, or other nested functions. Hooks are functions, but it\u2019s helpful to think of them as unconditional declarations about your component\u2019s needs. You \u201cuse\u201d React features at the top of your component similar to how you \u201cimport\u201d modules at the top of your file. <br><strong>Hooks \u2014\u2014\u4ee5 <code>use<\/code> \u5f00\u5934\u7684\u51fd\u6570\u2014\u2014\u53ea\u80fd\u5728\u7ec4\u4ef6\u6216<a href=\"https:\/\/zh-hans.react.dev\/learn\/reusing-logic-with-custom-hooks\">\u81ea\u5b9a\u4e49 Hook<\/a> \u7684\u6700\u9876\u5c42\u8c03\u7528\u3002<\/strong> \u4f60\u4e0d\u80fd\u5728\u6761\u4ef6\u8bed\u53e5\u3001\u5faa\u73af\u8bed\u53e5\u6216\u5176\u4ed6\u5d4c\u5957\u51fd\u6570\u5185\u8c03\u7528 Hook\u3002Hook \u662f\u51fd\u6570\uff0c\u4f46\u5c06\u5b83\u4eec\u89c6\u4e3a\u5173\u4e8e\u7ec4\u4ef6\u9700\u6c42\u7684\u65e0\u6761\u4ef6\u58f0\u660e\u4f1a\u5f88\u6709\u5e2e\u52a9\u3002\u5728\u7ec4\u4ef6\u9876\u90e8 \u201cuse\u201d React \u7279\u6027\uff0c\u7c7b\u4f3c\u4e8e\u5728\u6587\u4ef6\u9876\u90e8\u201c\u5bfc\u5165\u201d\u6a21\u5757\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"anatomy-of-usestate\">Anatomy of\u00a0<code>useState<\/code>\u00a0<a href=\"https:\/\/react.dev\/learn\/state-a-components-memory#anatomy-of-usestate\"><\/a>\uff08\u5256\u6790\u00a0<code>useState<\/code>\u00a0\uff09<\/h3>\n\n\n\n<p>When you call <a href=\"https:\/\/react.dev\/reference\/react\/useState\"><code>useState<\/code><\/a>, you are telling React that you want this component to remember something:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const &#91;index, setIndex] = useState(0);<\/code><\/pre>\n\n\n\n<p>In this case, you want React to remember <code>index<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Note<\/h3>\n\n\n\n<p><em><strong>The convention<\/strong>(\u60ef\u4f8b)<\/em> is to name this pair like <code>const [something, setSomething]<\/code>. You could name it anything you like, but conventions make things easier to understand across projects.\uff08\u60ef\u4f8b\u662f\u5c06\u8fd9\u5bf9\u8fd4\u56de\u503c\u547d\u540d\u4e3a <code>const [thing, setThing]<\/code>\u3002\u4f60\u4e5f\u53ef\u4ee5\u5c06\u5176\u547d\u540d\u4e3a\u4efb\u4f55\u4f60\u559c\u6b22\u7684\u540d\u79f0\uff0c\u4f46\u9075\u7167\u7ea6\u5b9a\u4fd7\u6210\u80fd\u4f7f\u8de8\u9879\u76ee\u5408\u4f5c\u66f4\u6613\u7406\u89e3\u3002\uff09<\/p>\n\n\n\n<p>The only argument(\u53c2\u6570) to <code>useState<\/code> is the <strong>initial value<\/strong> of your state variable. In this example, the <code>index<\/code>\u2019s initial value is set to <code>0<\/code> with <code>useState(0)<\/code>.<\/p>\n\n\n\n<p>Every time your component renders, <code>useState<\/code> gives you an array containing two values\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u6bcf\u6b21\u4f60\u7684\u7ec4\u4ef6\u6e32\u67d3\u65f6\uff0c<code>useState<\/code> \u90fd\u4f1a\u7ed9\u4f60\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u503c\u7684\u6570\u7ec4<\/mark><\/strong>\uff09:<\/p>\n\n\n\n<ol>\n<li>The\u00a0<strong>state variable<\/strong>\u00a0(<code>index<\/code>) with the value you stored.\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>state \u53d8\u91cf<\/strong>\u00a0(<code>index<\/code>) \u4f1a\u4fdd\u5b58\u4e0a\u6b21\u6e32\u67d3\u7684\u503c<\/mark>\uff09<\/li>\n\n\n\n<li>The\u00a0<strong>state setter function<\/strong>\u00a0(<code>setIndex<\/code>) which can update the state variable and trigger React to render the component again.\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>state setter \u51fd\u6570<\/strong>\u00a0(<code>setIndex<\/code>) \u53ef\u4ee5\u66f4\u65b0 state \u53d8\u91cf\u5e76\u89e6\u53d1 React \u91cd\u65b0\u6e32\u67d3\u7ec4\u4ef6<\/mark>\uff09<\/li>\n<\/ol>\n\n\n\n<p>Here\u2019s how that happens in action:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const &#91;index, setIndex] = useState(0);<\/code><\/pre>\n\n\n\n<ol>\n<li><strong>Your component renders the first time.<\/strong>\u00a0Because you passed\u00a0<code>0<\/code>\u00a0to\u00a0<code>useState<\/code>\u00a0as the initial value for\u00a0<code>index<\/code>, it will return\u00a0<code>[0, setIndex]<\/code>. React remembers\u00a0<code>0<\/code>\u00a0is the latest state value.<\/li>\n\n\n\n<li><strong>You update the state.<\/strong>\u00a0When a user clicks the button, it calls\u00a0<code>setIndex(index + 1)<\/code>.\u00a0<code>index<\/code>\u00a0is\u00a0<code>0<\/code>, so it\u2019s\u00a0<code>setIndex(1)<\/code>. <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">This tells React to remember\u00a0<code>index<\/code>\u00a0is\u00a0<code>1<\/code>\u00a0now and triggers another render<\/mark>.<\/li>\n\n\n\n<li><strong>Your component\u2019s second render.<\/strong>\u00a0React still sees\u00a0<code>useState(0)<\/code>, but because React\u00a0<em>remembers<\/em>\u00a0that you set\u00a0<code>index<\/code>\u00a0to\u00a0<code>1<\/code>, it returns\u00a0<code>[1, setIndex]<\/code>\u00a0instead.<\/li>\n\n\n\n<li>And so on!<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"giving-a-component-multiple-state-variables\">Giving a component multiple state variables&nbsp;<a href=\"https:\/\/react.dev\/learn\/state-a-components-memory#giving-a-component-multiple-state-variables\"><\/a><\/h2>\n\n\n\n<p>You can have as many state variables of as many types as you like in one component. This component has two state variables, a number <code>index<\/code> and a boolean <code>showMore<\/code> that\u2019s toggled when you click \u201cShow details\u201d:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\nimport { sculptureList } from '.\/data.js';\n\nexport default function Gallery() {\n  const &#91;index, setIndex] = useState(0);\n  const &#91;showMore, setShowMore] = useState(false);\n\n  function handleNextClick() {\n    setIndex(index + 1);\n  }\n\n  function handleMoreClick() {\n    setShowMore(!showMore);\n  }\n\n  let sculpture = sculptureList&#91;index];\n  return (\n    &lt;>\n      &lt;button onClick={handleNextClick}>\n        Next\n      &lt;\/button>\n      &lt;h2>\n        &lt;i>{sculpture.name} &lt;\/i> \n        by {sculpture.artist}\n      &lt;\/h2>\n      &lt;h3>  \n        ({index + 1} of {sculptureList.length})\n      &lt;\/h3>\n      &lt;button onClick={handleMoreClick}>\n        {showMore ? 'Hide' : 'Show'} details\n      &lt;\/button>\n      {showMore &amp;&amp; &lt;p>{sculpture.description}&lt;\/p>}\n      &lt;img \n        src={sculpture.url} \n        alt={sculpture.alt}\n      \/>\n    &lt;\/>\n  );\n}<\/code><\/pre>\n\n\n\n<p>It is a good idea to have multiple state variables if their state is unrelated, like <code>index<\/code> and <code>showMore<\/code> in this example. But if you find that you often change two state variables together, it might be easier to combine them into one. For example, if you have a form with many fields, it\u2019s more convenient to have a single state variable that holds an object than state variable per field. Read <a href=\"https:\/\/react.dev\/learn\/choosing-the-state-structure\">Choosing the State Structure<\/a> for more tips.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How does React know which state to return?\u00a0\uff08React \u5982\u4f55\u77e5\u9053\u8fd4\u56de\u54ea\u4e2a state\u00a0\uff1f\uff09<\/h2>\n\n\n\n<p>You might have noticed that the <code>useState<\/code> call does not receive any information about <em>which<\/em> state variable it refers to. There is no \u201cidentifier\u201d that is passed to <code>useState<\/code>, so how does it know which of the state variables to return? Does it rely on some magic like parsing your functions? The answer is no.<\/p>\n\n\n\n<p>Instead, to enable their concise syntax, Hooks <strong>rely on a stable call order on every render of the same component.<\/strong> This works well in practice because if you follow the rule above (\u201conly call Hooks at the top level\u201d), Hooks will always be called in the same order. Additionally, a <a href=\"https:\/\/www.npmjs.com\/package\/eslint-plugin-react-hooks\" target=\"_blank\" rel=\"noreferrer noopener\">linter plugin<\/a> catches most mistakes.<\/p>\n\n\n\n<p>Internally, React holds an array of state pairs for every component. It also maintains the current pair index, which is set to <code>0<\/code> before rendering. Each time you call <code>useState<\/code>, React gives you the next state pair and increments the index. You can read more about this mechanism in <a href=\"https:\/\/medium.com\/@ryardley\/react-hooks-not-magic-just-arrays-cd4f1857236e\" target=\"_blank\" rel=\"noreferrer noopener\">React Hooks: Not Magic, Just Arrays.<\/a><\/p>\n\n\n\n<p>This example <strong>doesn\u2019t use React<\/strong> but it gives you an idea of how <code>useState<\/code> works internally: \uff08\u5177\u4f53\u4f8b\u5b50\u89c1\uff1a<a href=\"https:\/\/zh-hans.react.dev\/learn\/state-a-components-memory\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/zh-hans.react.dev\/learn\/state-a-components-memory<\/a>\uff09<\/p>\n\n\n\n<p>\u4f60\u53ef\u80fd\u5df2\u7ecf\u6ce8\u610f\u5230\uff0c<code>useState<\/code> \u5728\u8c03\u7528\u65f6\u6ca1\u6709\u4efb\u4f55\u5173\u4e8e\u5b83\u5f15\u7528\u7684\u662f<em>\u54ea\u4e2a<\/em> state \u53d8\u91cf\u7684\u4fe1\u606f\u3002\u6ca1\u6709\u4f20\u9012\u7ed9 <code>useState<\/code> \u7684\u201c\u6807\u8bc6\u7b26\u201d\uff0c\u5b83\u662f\u5982\u4f55\u77e5\u9053\u8981\u8fd4\u56de\u54ea\u4e2a state \u53d8\u91cf\u5462\uff1f\u5b83\u662f\u5426\u4f9d\u8d56\u4e8e\u89e3\u6790\u51fd\u6570\u4e4b\u7c7b\u7684\u9b54\u6cd5\uff1f\u7b54\u6848\u662f\u5426\u5b9a\u7684\u3002<\/p>\n\n\n\n<p>\u76f8\u53cd\uff0c\u4e3a\u4e86\u4f7f\u8bed\u6cd5\u66f4\u7b80\u6d01\uff0c<strong>\u5728\u540c\u4e00\u7ec4\u4ef6\u7684\u6bcf\u6b21\u6e32\u67d3\u4e2d\uff0cHooks \u90fd\u4f9d\u6258\u4e8e\u4e00\u4e2a\u7a33\u5b9a\u7684\u8c03\u7528\u987a\u5e8f<\/strong>\u3002\u8fd9\u5728\u5b9e\u8df5\u4e2d\u5f88\u6709\u6548\uff0c\u56e0\u4e3a\u5982\u679c\u4f60\u9075\u5faa\u4e0a\u9762\u7684\u89c4\u5219\uff08\u201c\u53ea\u5728\u9876\u5c42\u8c03\u7528 Hooks\u201d\uff09\uff0cHooks \u5c06\u59cb\u7ec8\u4ee5\u76f8\u540c\u7684\u987a\u5e8f\u88ab\u8c03\u7528\u3002\u6b64\u5916\uff0c<a href=\"https:\/\/www.npmjs.com\/package\/eslint-plugin-react-hooks\" target=\"_blank\" rel=\"noreferrer noopener\">linter \u63d2\u4ef6<\/a>\u4e5f\u53ef\u4ee5\u6355\u83b7\u5927\u591a\u6570\u9519\u8bef\u3002<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u5728 React \u5185\u90e8\uff0c\u4e3a\u6bcf\u4e2a\u7ec4\u4ef6\u4fdd\u5b58\u4e86\u4e00\u4e2a\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e00\u9879\u90fd\u662f\u4e00\u4e2a state \u5bf9\u3002\u5b83\u7ef4\u62a4\u5f53\u524d state \u5bf9\u7684\u7d22\u5f15\u503c\uff0c\u5728\u6e32\u67d3\u4e4b\u524d\u5c06\u5176\u8bbe\u7f6e\u4e3a \u201c0\u201d\u3002\u6bcf\u6b21\u8c03\u7528 useState \u65f6\uff0cReact \u90fd\u4f1a\u4e3a\u4f60\u63d0\u4f9b\u4e00\u4e2a state \u5bf9\u5e76\u589e\u52a0\u7d22\u5f15\u503c\u3002<\/strong><\/mark>\u4f60\u53ef\u4ee5\u5728\u6587\u7ae0 <a href=\"https:\/\/medium.com\/@ryardley\/react-hooks-not-magic-just-arrays-cd4f1857236e\" target=\"_blank\" rel=\"noreferrer noopener\">React Hooks: not magic, just arrays<\/a>\u4e2d\u9605\u8bfb\u6709\u5173\u6b64\u673a\u5236\u7684\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"state-is-isolated-and-private\">State is isolated and private\u00a0<a href=\"https:\/\/react.dev\/learn\/state-a-components-memory#state-is-isolated-and-private\"><\/a>\uff08State \u662f\u9694\u79bb\u4e14\u79c1\u6709\u7684\uff09<\/h2>\n\n\n\n<p style=\"font-size:18px\">State is local to a <strong><em>component instance<\/em><\/strong> on the screen. In other words, <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">if you render the same component twice, each copy will have completely isolated state!<\/mark><\/strong> Changing one of them will not affect the other.<\/p>\n\n\n\n<p>In this example, the <code>Gallery<\/code> component from earlier is rendered twice with no changes to its logic.<\/p>\n\n\n\n<p>This is what makes state different from <strong><em>regular variables<\/em><\/strong> that you might declare at the top of your module\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u8fd9\u5c31\u662f state \u4e0e\u58f0\u660e\u5728\u6a21\u5757\u9876\u90e8\u7684<strong><em>\u666e\u901a\u53d8\u91cf<\/em><\/strong>\u4e0d\u540c\u7684\u539f\u56e0\u3002<\/mark>\uff09. State is not <strong><em>tied<\/em><\/strong> to a particular function call or a place in the code, but it\u2019s \u201clocal\u201d to the specific place on the screen\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">State \u4e0d\u4f9d\u8d56\u4e8e\u7279\u5b9a\u7684\u51fd\u6570\u8c03\u7528\u6216\u5728\u4ee3\u7801\u4e2d\u7684\u4f4d\u7f6e\uff0c\u5b83\u7684\u4f5c\u7528\u57df\u201c\u53ea\u9650\u4e8e\u201d\u5c4f\u5e55\u4e0a\u7684\u67d0\u5757\u7279\u5b9a\u533a\u57df\u3002<\/mark>\uff09. You rendered two <code>&lt;Gallery \/><\/code> components, so their state is stored separately\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4f60\u6e32\u67d3\u4e86\u4e24\u4e2a <code>&lt;Gallery \/><\/code> \u7ec4\u4ef6\uff0c\u6240\u4ee5\u5b83\u4eec\u7684 state \u662f\u5206\u522b\u5b58\u50a8\u7684<\/mark>\uff09.<\/p>\n\n\n\n<p>Also notice how the <code>Page<\/code> component doesn\u2019t \u201cknow\u201d anything about the <code>Gallery<\/code> state or even whether it has any. Unlike props, <strong>state is fully private to the component declaring it.<\/strong> The parent component can\u2019t change it. This lets you add state to any component or remove it without impacting the rest of the components.<\/p>\n\n\n\n<p>What if you wanted both galleries to keep their states in sync? The right way to do it in React is to <em>remove<\/em> state from child components and add it to their closest shared parent. The next few pages will focus on organizing state of a single component, but we will return to this topic in <a href=\"https:\/\/react.dev\/learn\/sharing-state-between-components\">Sharing State Between Components.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"recap\">Recap<a href=\"https:\/\/react.dev\/learn\/state-a-components-memory#recap\"><\/a><\/h2>\n\n\n\n<ul>\n<li>Use a state variable when a component needs to \u201cremember\u201d some information between <strong><em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">renders(\u591a\u6b21\u6e32\u67d3)<\/mark><\/em><\/strong>.<\/li>\n\n\n\n<li>State variables are declared by calling the\u00a0<code>useState<\/code>\u00a0Hook.<\/li>\n\n\n\n<li>Hooks are special functions that start with\u00a0<code>use<\/code>. They let you \u201chook into\u201d React features like state.<\/li>\n\n\n\n<li>Hooks might remind you of imports: they need to be called unconditionally. Calling Hooks, including\u00a0<code>useState<\/code>, is only valid at the top level of a component or another Hook.<\/li>\n\n\n\n<li>The\u00a0<code>useState<\/code>\u00a0Hook returns a pair of values: the current state and the function to update it.<\/li>\n\n\n\n<li>You can have more than one state variable. Internally, React matches them up by their order.<\/li>\n\n\n\n<li>State is private to the component. If you render it in two places, each copy gets its own state.<\/li>\n\n\n\n<li>\u5f53\u4e00\u4e2a\u7ec4\u4ef6\u9700\u8981\u5728\u591a\u6b21\u6e32\u67d3\u95f4\u201c\u8bb0\u4f4f\u201d\u67d0\u4e9b\u4fe1\u606f\u65f6\u4f7f\u7528 state \u53d8\u91cf\u3002<\/li>\n\n\n\n<li>State \u53d8\u91cf\u662f\u901a\u8fc7\u8c03\u7528\u00a0<code>useState<\/code>\u00a0Hook \u6765\u58f0\u660e\u7684\u3002<\/li>\n\n\n\n<li>Hook \u662f\u4ee5\u00a0<code>use<\/code>\u00a0\u5f00\u5934\u7684\u7279\u6b8a\u51fd\u6570\u3002\u5b83\u4eec\u80fd\u8ba9\u4f60 \u201chook\u201d \u5230\u50cf state \u8fd9\u6837\u7684 React \u7279\u6027\u4e2d\u3002<\/li>\n\n\n\n<li>Hook \u53ef\u80fd\u4f1a\u8ba9\u4f60\u60f3\u8d77 import\uff1a\u5b83\u4eec\u9700\u8981\u5728\u975e\u6761\u4ef6\u8bed\u53e5\u4e2d\u8c03\u7528\u3002\u8c03\u7528 Hook \u65f6\uff0c\u5305\u62ec\u00a0<code>useState<\/code>\uff0c\u4ec5\u5728\u7ec4\u4ef6\u6216\u53e6\u4e00\u4e2a Hook \u7684\u9876\u5c42\u88ab\u8c03\u7528\u624d\u6709\u6548\u3002<\/li>\n\n\n\n<li><code>useState<\/code>\u00a0Hook \u8fd4\u56de\u4e00\u5bf9\u503c\uff1a\u5f53\u524d state \u548c\u66f4\u65b0\u5b83\u7684\u51fd\u6570\u3002<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u62e5\u6709\u591a\u4e2a state \u53d8\u91cf\u3002\u5728\u5185\u90e8\uff0cReact \u6309\u987a\u5e8f\u5339\u914d\u5b83\u4eec\u3002<\/li>\n\n\n\n<li>State \u662f\u7ec4\u4ef6\u79c1\u6709\u7684\u3002\u5982\u679c\u4f60\u5728\u4e24\u4e2a\u5730\u65b9\u6e32\u67d3\u5b83\uff0c\u5219\u6bcf\u4e2a\u526f\u672c\u90fd\u6709\u72ec\u5c5e\u4e8e\u81ea\u5df1\u7684 state\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53c2\uff1a<a href=\"https:\/\/zh-hans.react.dev\/learn\/state-a-components-memory\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/zh-hans.react.dev\/learn\/state-a-components-memory<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>State \u5c31\u662f\u7ec4\u4ef6\u7684\u8bb0\u5fc6\u3002 Components often need to change what\u2019s o&#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>State: A Component&#039;s Memory - \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\/28\/state-a-components-memory\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"State: A Component&#039;s Memory - \u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"State \u5c31\u662f\u7ec4\u4ef6\u7684\u8bb0\u5fc6\u3002 Components often need to change what\u2019s o...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-28T07:08:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-28T07:08:19+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=\"11 \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\/28\/state-a-components-memory\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/\"},\"author\":{\"name\":\"\u7801\u5148\u751f\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"headline\":\"State: A Component&#8217;s Memory\",\"datePublished\":\"2024-07-28T07:08:18+00:00\",\"dateModified\":\"2024-07-28T07:08:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/\"},\"wordCount\":1414,\"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\/28\/state-a-components-memory\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/\",\"url\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/\",\"name\":\"State: A Component's Memory - \u7801\u5148\u751f\u7684\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/#website\"},\"datePublished\":\"2024-07-28T07:08:18+00:00\",\"dateModified\":\"2024-07-28T07:08:19+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/codermr.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"State: A Component&#8217;s Memory\"}]},{\"@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":"State: A Component's Memory - \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\/28\/state-a-components-memory\/","og_locale":"zh_CN","og_type":"article","og_title":"State: A Component's Memory - \u7801\u5148\u751f\u7684\u535a\u5ba2","og_description":"State \u5c31\u662f\u7ec4\u4ef6\u7684\u8bb0\u5fc6\u3002 Components often need to change what\u2019s o...","og_url":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/","og_site_name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","article_published_time":"2024-07-28T07:08:18+00:00","article_modified_time":"2024-07-28T07:08:19+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":"11 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/#article","isPartOf":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/"},"author":{"name":"\u7801\u5148\u751f","@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"headline":"State: A Component&#8217;s Memory","datePublished":"2024-07-28T07:08:18+00:00","dateModified":"2024-07-28T07:08:19+00:00","mainEntityOfPage":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/"},"wordCount":1414,"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\/28\/state-a-components-memory\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/","url":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/","name":"State: A Component's Memory - \u7801\u5148\u751f\u7684\u535a\u5ba2","isPartOf":{"@id":"http:\/\/codermr.com\/#website"},"datePublished":"2024-07-28T07:08:18+00:00","dateModified":"2024-07-28T07:08:19+00:00","breadcrumb":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/state-a-components-memory\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/codermr.com\/"},{"@type":"ListItem","position":2,"name":"State: A Component&#8217;s Memory"}]},{"@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\/1241"}],"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=1241"}],"version-history":[{"count":9,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1241\/revisions"}],"predecessor-version":[{"id":1253,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1241\/revisions\/1253"}],"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=1241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/categories?post=1241"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/tags?post=1241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}