{"id":1192,"date":"2024-07-28T01:14:55","date_gmt":"2024-07-27T17:14:55","guid":{"rendered":"http:\/\/codermr.com\/?p=1192"},"modified":"2024-07-28T11:29:32","modified_gmt":"2024-07-28T03:29:32","slug":"react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/","title":{"rendered":"React Basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">This chapter&#8217;s Objectives\uff1a<\/h1>\n\n\n\n<ul>\n<li>How to create and nest components<\/li>\n\n\n\n<li>How to add markup and styles<\/li>\n\n\n\n<li>How to display data<\/li>\n\n\n\n<li>How to render conditions and lists<\/li>\n\n\n\n<li>How to respond to events and update the screen<\/li>\n\n\n\n<li>How to share data between components<\/li>\n<\/ul>\n\n\n\n<p><em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">render \u6e32\u67d3\uff1b respond \u54cd\u5e94\uff1bmarkup \u6807\u7b7e<\/mark><\/em><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"components\">Creating and nesting components&nbsp;<a href=\"https:\/\/react.dev\/learn#components\"><\/a><\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\u7b2c\u4e00\u4e2a\u6982\u5ff5\uff1a\u4ec0\u4e48\u662f\u7ec4\u4ef6\uff08component\uff09\uff1f<\/h3>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>React apps are made out of <em>components<\/em>.<\/strong><\/mark> A component is a piece of the UI (user interface) that has its own logic and appearance. A component can be as small as a button, or as large as an entire page.<\/p>\n\n\n\n<p>React components are JavaScript functions that return markup\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>React \u7ec4\u4ef6\u662f\u8fd4\u56de\u6807\u7b7e\u7684 JavaScript \u51fd\u6570<\/strong><\/mark>\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function MyButton() {\n  return (\n    &lt;button&gt;I'm a button&lt;\/button&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<p>Now that you\u2019ve declared <code>MyButton<\/code>, you can nest it into another component\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u7ec4\u4ef6\u53ef\u4ee5\u5d4c\u5957<\/mark><\/strong>\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default function MyApp() {\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Welcome to my app&lt;\/h1&gt;\n      &lt;MyButton \/&gt;\n    &lt;\/div&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<p>Notice that <code>&lt;MyButton \/&gt;<\/code> starts with a capital letter. That\u2019s how you know it\u2019s a React component. React component names must always start with a capital letter, while HTML tags must be lowercase.\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">React \u7ec4\u4ef6\u547d\u540d\u5fc5\u987b\u7528\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff0cHTML \u6807\u7b7e\u547d\u540d\u5fc5\u987b\u7528\u5c0f\u5199\u5b57\u7b26\u5f00\u5934<\/mark><\/strong>\uff09<\/p>\n\n\n\n<p>Have a look at the result :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function MyButton() {\n  return (\n    &lt;button&gt;\n      I'm a button\n    &lt;\/button&gt;\n  );\n}\n\nexport default function MyApp() {\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Welcome to my app&lt;\/h1&gt;\n      &lt;MyButton \/&gt;\n    &lt;\/div&gt;\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-55.png\"><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"109\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-55.png\" alt=\"\" class=\"wp-image-1194\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-55.png 522w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-55-300x63.png 300w\" sizes=\"(max-width: 522px) 100vw, 522px\" \/><\/a><\/figure>\n\n\n\n<p>The <code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">export default<\/mark><\/strong><\/code> keywords specify the main component in the file\uff08<code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">export default<\/mark><\/strong><\/code> \u6307\u5b9a\u4e86\u8be5\u7ec4\u4ef6\u662f\u4e3b\u7ec4\u4ef6\uff09.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"writing-markup-with-jsx\">Writing markup with JSX&nbsp;<a href=\"https:\/\/react.dev\/learn#writing-markup-with-jsx\"><\/a><\/h2>\n\n\n\n<p>The markup syntax you\u2019ve seen above is called <em>JSX<\/em>. It is optional, but most React projects use JSX for its convenience.\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4e0a\u9762\u4f8b\u5b50\u4e2d\u7684\u8bed\u6cd5\u53eb\u505a JSX<\/mark><\/strong>\uff09\u3002JSX is stricter than HTML. You have to close tags like <code>&lt;br \/&gt;<\/code>.<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"> Your component also can\u2019t return multiple JSX tags<\/mark><\/strong>. You have to wrap them into a shared parent, like a <code>&lt;div&gt;...&lt;\/div&gt;<\/code> or an empty <code>&lt;&gt;...&lt;\/&gt;<\/code> wrapper\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u91cd\u70b9\uff1a\u4e00\u4e2a\u7ec4\u4ef6\u4e0d\u80fd\u8fd4\u56de\u591a\u4e2a\u6807\u7b7e\uff0c\u5982\u679c\u8981\u8fd4\u56de\u591a\u4e2a\u6807\u7b7e\uff0c\u4f60\u53ef\u4ee5\u7528\u53e6\u5916\u4e00\u4e2a\u6807\u7b7e\u5c06\u5b83\u4eec\u5305\u8d77\u6765\uff0c\u5982\u4e0b\u6240\u793a<\/mark><\/strong>\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function AboutPage() {\n  return (\n    &lt;&gt;\n      &lt;h1&gt;About&lt;\/h1&gt;\n      &lt;p&gt;Hello there.&lt;br \/&gt;How do you do?&lt;\/p&gt;\n    &lt;\/&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50 return \u8fd4\u56de\u7684\u662f\u4e00\u4e2a &lt;&gt;  &#8230;  &lt;\/&gt; \u6807\u7b7e\uff0c\u4e0d\u7528\u5927\u60ca\u5c0f\u602a\uff0c\u5982\u679c\u4f60\u4e0d\u60f3\u5728\u6807\u7b7e\u4e2d\u589e\u52a0\u4e00\u4e2a\u989d\u5916\u7684 &lt;div&gt;\uff0c\u53ef\u4ee5\u7528 &lt;&gt; \u548c &lt;\/&gt; \u5143\u7d20\u6765\u4ee3\u66ff\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"adding-styles\">Adding styles&nbsp;<a href=\"https:\/\/react.dev\/learn#adding-styles\"><\/a>\uff08\u6837\u5f0f\uff09<\/h2>\n\n\n\n<p>In React, you specify a CSS class with <code>className<\/code>. It works the same way as the HTML <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Global_attributes\/class\" target=\"_blank\" rel=\"noreferrer noopener\"><code>class<\/code><\/a> attribute, You can write the CSS rules for it in a separate CSS file:\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5728 React \u4e2d\u7ed9\u5143\u7d20\u6307\u5b9a\u6837\u5f0f\u4f7f\u7528 React \u7279\u5b9a\u7684\u5173\u952e\u5b57 <code>className<\/code> \uff0c\u4f60\u53ef\u4ee5\u5c06\u6837\u5f0f\u653e\u5728\u5355\u72ec\u7684\u4e00\u4e2a CSS \u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u4f7f\u7528 <code>className<\/code> \u5173\u952e\u5b57\u7ed9\u67d0\u4e2a\u5143\u7d20\u6dfb\u52a0\u6837\u5f0f<\/mark><\/strong>\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* In your CSS *\/\n.avatar {\n  border-radius: 50%;\n}\n\/* \u4f7f\u7528 className \u5173\u952e\u5b57\u7ed9\u67d0\u4e2a\u5143\u7d20\u6dfb\u52a0\u6837\u5f0f *\/&lt;img className=\"avatar\" \/&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"displaying-data\">Displaying data&nbsp;<a href=\"https:\/\/react.dev\/learn#displaying-data\"><\/a>\uff08\u663e\u793a\u6570\u636e\uff09<\/h2>\n\n\n\n<p>JSX lets you put markup into JavaScript. <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Curly braces<\/mark> let you \u201cescape back\u201d into JavaScript so that you can embed some variable from your code and display it to the user. For example, this will display <code>user.name<\/code> \uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">JXF \u5141\u8bb8\u4f60\u5c06\u6807\u7b7e\u653e\u5230 JavaScript \u4e2d\u3002\u800c\u5927\u62ec\u53f7\u4f1a\u8ba9\u4f60 \u201c\u56de\u5230\u201d JavaScript \u4e2d\uff0c\u8fd9\u6837\u4f60\u5c31\u53ef\u4ee5\u4ece\u4f60\u7684\u4ee3\u7801\u4e2d\u5d4c\u5165\u4e00\u4e9b\u53d8\u91cf\u5e76\u5c55\u793a\u7ed9\u7528\u6237\u3002==\u300b\u610f\u601d\u4f60\u53ef\u4ee5\u5728 JSF \u8bed\u6cd5\u4e2d\u4f7f\u7528 js\uff0c\u53ea\u9700\u8981\u7528\u5927\u62ec\u53f7\u62ec\u8d77\u6765\u3002\u5982\u4e0b {user.name} \u4f1a\u8bfb\u53d6 js \u4e2d\u7684 user \u53d8\u91cf\u4e2d\u7684 name \u5c5e\u6027\u3002<\/mark><\/strong>\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return (\n  &lt;h1&gt;\n    {user.name}\n  &lt;\/h1&gt;\n);<\/code><\/pre>\n\n\n\n<p>You can also \u201cescape into JavaScript\u201d from JSX attributes, but you have to use curly braces <em>instead of<\/em> quotes. For example, <code>className=\"avatar\"<\/code> passes the <code>\"avatar\"<\/code> string as the CSS class, but <code>src={user.imageUrl}<\/code> reads the JavaScript <code>user.imageUrl<\/code> variable value, and then passes that value as the <code>src<\/code> attribute\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4f60\u8fd8\u53ef\u4ee5\u5c06 JSX \u5c5e\u6027 \u201c\u8f6c\u4e49\u5230 JavaScript\u201d\uff0c\u4f46\u4f60\u5fc5\u987b\u4f7f\u7528\u5927\u62ec\u53f7 <strong>\u800c\u975e<\/strong> \u5f15\u53f7\u3002\u4f8b\u5982\uff0c<code>className=\"avatar\"<\/code> \u662f\u5c06 <code>\"avatar\"<\/code> \u5b57\u7b26\u4e32\u4f20\u9012\u7ed9 <code>className<\/code>\uff0c\u4f5c\u4e3a CSS \u7684 class\u3002\u4f46 <code>src={user.imageUrl}<\/code> \u4f1a\u8bfb\u53d6 JavaScript \u7684 <code>user.imageUrl<\/code> \u53d8\u91cf\uff0c\u7136\u540e\u5c06\u8be5\u503c\u4f5c\u4e3a <code>src<\/code> \u5c5e\u6027\u4f20\u9012\uff1a<\/mark>\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return (\n  &lt;img\n    className=\"avatar\"\n    src={user.imageUrl}\n  \/&gt;\n);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u795e\u5947\u7684\u5927\u62ec\u53f7\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Curly brace<\/strong><\/mark>\uff09\uff1a<strong>\u4e00\u6247\u8fdb\u5165 JavaScript \u4e16\u754c\u7684\u7a97\u6237<\/strong><\/h3>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u5927\u62ec\u53f7\u5185\u7684\u4efb\u4f55 JavaScript \u8868\u8fbe\u5f0f\u90fd\u80fd\u6b63\u5e38\u8fd0\u884c<\/mark><\/strong>\uff0c\u5305\u62ec\u50cf formatDate() \u8fd9\u6837\u7684\u51fd\u6570\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const today = new Date();\n\nfunction formatDate(date) {\n  return new Intl.DateTimeFormat(\n    'zh-CN',\n    { weekday: 'long' }\n  ).format(date);\n}\n\nexport default function TodoList() {\n  return (\n    &lt;h1&gt;To Do List for {formatDate(today)}&lt;\/h1&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u53ef\u4ee5\u5728\u54ea\u4f7f\u7528\u5927\u62ec\u53f7<\/strong>\uff1f<\/h3>\n\n\n\n<p>\u5728 JSX \u4e2d\uff0c\u53ea\u80fd\u5728\u4ee5\u4e0b\u4e24\u79cd\u573a\u666f\u4e2d\u4f7f\u7528\u5927\u62ec\u53f7\uff1a<\/p>\n\n\n\n<ol>\n<li>\u7528\u4f5c JSX \u6807\u7b7e\u5185\u7684\u6587\u672c\uff1a{name}&#8217;s To Do List \u662f\u6709\u6548\u7684\uff0c\u4f46\u662f &lt;{tag}&gt;Gregorio Y. Zara&#8217;s To Do List \u65e0\u6548\u3002<\/li>\n\n\n\n<li>\u7528\u4f5c\u7d27\u8ddf\u5728 = \u7b26\u53f7\u540e\u7684 \u5c5e\u6027\uff1asrc={avatar} \u4f1a\u8bfb\u53d6 avatar \u53d8\u91cf\uff0c\u4f46\u662f src=&#8221;{avatar}&#8221; \u53ea\u4f1a\u4f20\u4e00\u4e2a\u5b57\u7b26\u4e32 {avatar}\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u53cc\u5927\u62ec\u53f7<\/h3>\n\n\n\n<p>\u9664\u4e86\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u548c\u5176\u5b83 JavaScript \u8868\u8fbe\u5f0f\uff0c\u4f60\u751a\u81f3\u53ef\u4ee5\u5728 JSX \u4e2d\u4f20\u9012\u5bf9\u8c61\u3002<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u5bf9\u8c61\u4e5f\u7528\u5927\u62ec\u53f7\u8868\u793a\uff0c\u4f8b\u5982 { name: &#8220;Hedy Lamarr&#8221;, inventions: 5 }\u3002\u56e0\u6b64\uff0c\u4e3a\u4e86\u80fd\u5728 JSX \u4e2d\u4f20\u9012\uff0c\u4f60\u5fc5\u987b\u7528\u53e6\u4e00\u5bf9\u989d\u5916\u7684\u5927\u62ec\u53f7\u5305\u88f9\u5bf9\u8c61\uff1aperson={{ name: &#8220;Hedy Lamarr&#8221;, inventions: 5 }}\u3002<\/strong><\/mark><\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u6240\u4ee5\u5f53\u4f60\u4e0b\u6b21\u5728 JSX \u4e2d\u770b\u5230 {{ \u548c }}\u65f6\uff0c\u5c31\u77e5\u9053\u5b83\u53ea\u4e0d\u8fc7\u662f\u5305\u5728\u5927\u62ec\u53f7\u91cc\u7684\u4e00\u4e2a\u5bf9\u8c61\u7f62\u4e86\uff01<\/mark><\/strong><\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/* \u8fd9\u662f\u4e00\u4e2a js \u53d8\u91cf\uff0c\u53ef\u4ee5\u5728 JSF \u4e2d\u901a\u8fc7\u5927\u62ec\u53f7\u5f15\u8d77 js \u53d8\u91cf\u7684\u503c *\/\nconst user = { \n  name: 'Hedy Lamarr',\n  imageUrl: 'https:\/\/i.imgur.com\/yXOvdOSs.jpg',\n  imageSize: 90,\n};\n\nexport default function Profile() {\n  return (\n    &lt;&gt;\n      &lt;h1&gt;{user.name}&lt;\/h1&gt;\n      &lt;img\n        className=\"avatar\"\n        src={user.imageUrl}\n        alt={'Photo of ' + user.name}\n        style={{\n          width: user.imageSize,\n          height: user.imageSize\n        }}\n      \/&gt;\n    &lt;\/&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conditional-rendering\">Conditional rendering&nbsp;<a href=\"https:\/\/react.dev\/learn#conditional-rendering\"><\/a>\uff08\u6761\u4ef6\u6e32\u67d3\uff09<\/h2>\n\n\n\n<p>In React, there is no special syntax for writing conditions. Instead, you\u2019ll use the same techniques as you use when writing regular JavaScript code. For example, you can use an <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/if...else\" target=\"_blank\" rel=\"noreferrer noopener\"><code>if<\/code><\/a> statement to conditionally include JSX\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">React \u6ca1\u6709\u7279\u6b8a\u7684\u8bed\u6cd5\u6765\u7f16\u5199\u6761\u4ef6\u8bed\u53e5\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 JavaScript \u4e2d\u7684\u6761\u4ef6\u8bed\u53e5\u3002\u4f8b\u5982\u4f7f\u7528 <a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/JavaScript\/Reference\/Statements\/if...else\" target=\"_blank\" rel=\"noreferrer noopener\"><code>if<\/code><\/a> \u8bed\u53e5\u6839\u636e\u6761\u4ef6\u5f15\u5165 JSX\uff1a<\/mark>\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let content;\nif (isLoggedIn) {\n  content = &lt;AdminPanel \/&gt;;\n} else {\n  content = &lt;LoginForm \/&gt;;\n}\nreturn (\n  &lt;div&gt;\n    {content}\n  &lt;\/div&gt;\n);<\/code><\/pre>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u2588\u2588<\/mark><\/strong> <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\u5f3a\u70c8\u6ce8\u610f\u4e24\u70b9 \u2588\u2588\uff1a<\/mark><\/strong><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\uff081\uff09\u4e0a\u9762\u4f8b\u5b50\u4e2d\uff0c\u4f60\u7684\u6761\u4ef6\u8bed\u53e5 if &#8230; else &#8230; \u662f\u5199\u5728 return ( &#8230; ) \u5916\u9762\u7684\uff0c\u5728 return ( &#8230; ) \u91cc\u9762\u597d\u50cf\u662f\u6ca1\u6cd5\u4f7f\u7528 if &#8230; else &#8230; \u6761\u4ef6\u8bed\u53e5\u7684\u3002<\/mark><\/strong><br><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">\uff082\uff09\u4f60\u7684 if &#8230; else &#8230; \u662f\u5728\u5199\u5728\u5927\u62ec\u53f7\u91cc\u9762\u7684\uff01\uff01\uff01<\/mark><\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u66f4\u591a\u6761\u4ef6\u8bed\u53e5\u5199\u6cd5\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<p>If you prefer more <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">compact<\/mark> code, you can use the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Conditional_Operator\" target=\"_blank\" rel=\"noreferrer noopener\">conditional <code>?<\/code> operator.<\/a> Unlike <code>if<\/code>, it works inside JSX:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div&gt;\n  {isLoggedIn ? (\n    &lt;AdminPanel \/&gt;\n  ) : (\n    &lt;LoginForm \/&gt;\n  )}\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<p>When you don\u2019t need the <code>else<\/code> branch, you can also use a shorter <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Logical_AND#short-circuit_evaluation\" target=\"_blank\" rel=\"noreferrer noopener\">logical <code>&amp;&amp;<\/code> syntax<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div&gt;\n  {isLoggedIn &amp;&amp; &lt;AdminPanel \/&gt;}\n&lt;\/div&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"rendering-lists\">Rendering lists&nbsp;<a href=\"https:\/\/react.dev\/learn#rendering-lists\"><\/a>\uff08\u6e32\u67d3\u5217\u8868\uff09<\/h2>\n\n\n\n<p>You will <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">rely<\/mark> on JavaScript features like <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/for\" target=\"_blank\" rel=\"noreferrer noopener\"><code>for<\/code> loop<\/a> and the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/map\" target=\"_blank\" rel=\"noreferrer noopener\">array <code>map()<\/code> function<\/a> to render lists of components.<\/p>\n\n\n\n<p>For example, let\u2019s say you have an array of products , you can use the <code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">map()<\/mark><\/strong><\/code> function to transform an array of products into an array of <code>&lt;li&gt;<\/code> items :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const products = &#91;\n  { title: 'Cabbage', id: 1 },\n  { title: 'Garlic', id: 2 },\n  { title: 'Apple', id: 3 },\n];\n\nconst listItems = products.map(product =&gt;\n  &lt;li key={product.id}&gt;\n    {product.title}\n  &lt;\/li&gt;\n);\n\nreturn (\n  &lt;ul&gt;{listItems}&lt;\/ul&gt;\n);<\/code><\/pre>\n\n\n\n<p>Notice how <code>&lt;li&gt;<\/code> has a <code>key<\/code> attribute. For each item in a list, you should pass a string or a number that uniquely identifies that item among its siblings. Usually, a key should be coming from your data, such as a database ID. React uses your keys to know what happened if you later insert, delete, or reorder the items.(<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4e0a\u9762\u4f8b\u5b50\u4f7f\u7528 .map \u904d\u5386\u6570\u7ec4 products \u65f6\uff0c\u4f7f\u7528\u4e86\u4e2a\u53eb key \u7684\u5c5e\u6027\uff0c\u4e3a\u4ec0\u4e48\u4f1a\u6709\u8fd9\u4e2a key \u5c5e\u6027\u5462\uff1f\u662f\u4e3a\u4e86\u7528 key \u6807\u8bb0\u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u552f\u4e00\u6027\uff0c\u6240\u4ee5\u4f60\u5e94\u8be5\u4f7f\u7528\u552f\u4e00\u7684\u3001\u4e0d\u4f1a\u91cd\u590d\u7684\u503c\u4f5c\u4e3a key \u7684\u503c\u3002<\/mark><\/strong>)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"responding-to-events\">Responding to events&nbsp;<a href=\"https:\/\/react.dev\/learn#responding-to-events\"><\/a>\uff08\u54cd\u5e94\u4e8b\u4ef6\uff09<\/h2>\n\n\n\n<p>You can respond to events by declaring <em>event handler<\/em> functions inside your components\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4f60\u53ef\u4ee5\u901a\u8fc7\u5728\u7ec4\u4ef6\u4e2d\u58f0\u660e <strong>\u4e8b\u4ef6\u5904\u7406<\/strong> \u51fd\u6570\u6765\u54cd\u5e94\u4e8b\u4ef6<\/mark>\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function MyButton() {\n  \/* \u4e8b\u4ef6\u5904\u7406\u51fd\u6570 *\/\n  function handleClick() {\n    alert('You clicked me!');\n  }\n\n  return (\n    &lt;button onClick={handleClick}&gt;\n      Click me\n    &lt;\/button&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<p>Notice how <code>onClick={handleClick}<\/code> has no <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">parentheses<\/mark><\/strong> at the end! Do not <em>call<\/em> the event handler function: you only need to <em>pass it down<\/em>. React will call your event handler when the user clicks the button.\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u6ce8\u610f\u4e09\u70b9\uff1a\u7b2c\u4e00\uff0c<code>onClick={handleClick}<\/code> \u662f\u7528\u5927\u62ec\u53f7\u5305\u4f4f \u7684\u3002\u7b2c\u4e8c\uff0c<code>handleClick<\/code> \u540e\u9762\u6ca1\u6709\u5c0f\u62ec\u53f7\u3002\u7b2c\u4e09\uff0c\u4f60\u4e0d\u9700\u8981\u4e3b\u52a8\u8c03\u7528\u8fd9\u4e2a\u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u5f53\u6709\u70b9\u51fb\u4e8b\u4ef6\u65f6\uff0cReact \u4f1a\u81ea\u52a8\u8c03\u7528\u3002<\/mark><\/strong>\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"updating-the-screen\">Updating the screen&nbsp;<a href=\"https:\/\/react.dev\/learn#updating-the-screen\"><\/a>\uff08\u66f4\u65b0\u754c\u9762\uff09<\/h2>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">Often<\/mark>\uff08\u901a\u5e38\uff09, you\u2019ll want your component to \u201cremember\u201d some information and display it. For example, maybe you want to count the number of times a button is clicked. To do this, add <em><strong>state<\/strong><\/em> to your component.<\/p>\n\n\n\n<p>First, import <a href=\"https:\/\/react.dev\/reference\/react\/useState\"><code>useState<\/code><\/a> from React:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';<\/code><\/pre>\n\n\n\n<p>Now you can declare a <em>state variable<\/em> inside your component:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function MyButton() {\n  const &#91;count, setCount] = useState(0);\n  \/\/ ...<\/code><\/pre>\n\n\n\n<p>You\u2019ll get two things from <code>useState<\/code>: the current state (<code>count<\/code>), and the function that lets you update it (<code>setCount<\/code>). You can give them any names, but the convention is to write <code>[something, setSomething]<\/code>.<\/p>\n\n\n\n<p>The first time the button is displayed, <code>count<\/code> will be <code>0<\/code> because you passed <code>0<\/code> to <code>useState()<\/code>. When you want to change state, call <code>setCount()<\/code> and pass the new value to it. Clicking this button will increment the counter:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function MyButton() {\n  const &#91;count, setCount] = useState(0);\n\n  function handleClick() {\n    setCount(count + 1);\n  }\n\n  return (\n    &lt;button onClick={handleClick}&gt;\n      Clicked {count} times\n    &lt;\/button&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5982\u4e0a\uff0c<code>const [count, setCount] = useState(0);<\/code> \u91cc\u9762\u7684 <\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><code>count<\/code> <\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u662f\u4e00\u4e2a\u5c5e\u6027\uff0c\u9ed8\u8ba4\u503c\u7531 <\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><code>useState(0)<\/code><\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"> \u5df2\u7ecf\u6307\u5b9a\u4e86\u662f 0 \uff0c\u800c <\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><code>setCount<\/code> <\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u662f\u4e00\u4e2a\u51fd\u6570\u540d\uff0c\u5982\u679c\u9700\u8981\u66f4\u65b0 <\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><code>count<\/code> <\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u7684\u503c\u9700\u8981\u8c03\u7528\u8fd9\u4e2a <\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><code>setCount<\/code> <\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u51fd\u6570\u3002==\u300b\u8fd9\u79cd\u8bed\u6cd5\u548c\u5199\u6cd5\u521d\u770b\u53ef\u80fd\u89c9\u5f97\u6709\u70b9\u5947\u602a\uff0c\u6162\u6162\u9002\u5e94\u4e86\u5c31\u597d\u4e86\u3002<\/mark><\/strong><\/p>\n\n\n\n<p><strong>\u9700\u8981\u6ce8\u610f\uff1a<\/strong>If you render the same component multiple times, each will get its own state. Click each button separately:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\n\nexport default function MyApp() {\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Counters that update separately&lt;\/h1&gt;\n      &lt;MyButton \/&gt;\n      &lt;MyButton \/&gt;\n    &lt;\/div&gt;\n  );\n}\n\nfunction MyButton() {\n  const &#91;count, setCount] = useState(0);\n\n  function handleClick() {\n    setCount(count + 1);\n  }\n\n  return (\n    &lt;button onClick={handleClick}&gt;\n      Clicked {count} times\n    &lt;\/button&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<p>Notice how each button \u201cremembers\u201d its own <code>count<\/code> state and doesn\u2019t affect other buttons.<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5982\u4e0a\uff0c&lt;div&gt; \u4e2d\u6709\u4e24\u4e2a &lt;MyButton \/&gt; \u7ec4\u4ef6\u5bf9\u8c61\uff0c\u5219\u6bcf\u4e2a\u7ec4\u4ef6\u5bf9\u8c61\u90fd\u6709\u81ea\u5df1\u7684 count \u503c\u3002<\/mark><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"using-hooks\">Using Hooks&nbsp;<a href=\"https:\/\/react.dev\/learn#using-hooks\"><\/a>\uff08\u4f7f\u7528\u94a9\u5b50\uff09<\/h2>\n\n\n\n<p>Functions starting with <code>use<\/code> are called <em>Hooks<\/em>. <code>useState<\/code> is a built-in Hook provided by React. You can find other built-in Hooks in the <a href=\"https:\/\/react.dev\/reference\/react\">API reference.<\/a> You can also write your own Hooks by combining the existing ones.\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4ee5 <code>use<\/code> \u5f00\u5934\u7684\u51fd\u6570\u88ab\u79f0\u4e3a Hook\u3002<code>useState<\/code> \u662f React \u63d0\u4f9b\u7684\u4e00\u4e2a\u5185\u7f6e Hook\u3002<\/mark><\/strong>\uff09<\/p>\n\n\n\n<p>Hooks are more restrictive than other functions. You can only call Hooks <em>at the top<\/em> of your components (or other Hooks). If you want to use <code>useState<\/code> in a condition or a loop, extract a new component and put it there.\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">Hook \u6bd4\u666e\u901a\u51fd\u6570\u66f4\u4e3a\u4e25\u683c\u3002\u4f60\u53ea\u80fd\u5728\u4f60\u7684\u7ec4\u4ef6\uff08\u6216\u5176\u4ed6 Hook\uff09\u7684 \u9876\u5c42 \u8c03\u7528 Hook\u3002\u5982\u679c\u4f60\u60f3\u5728\u4e00\u4e2a\u6761\u4ef6\u6216\u5faa\u73af\u4e2d\u4f7f\u7528 <code>useState<\/code>\uff0c\u8bf7\u63d0\u53d6\u4e00\u4e2a\u65b0\u7684\u7ec4\u4ef6\u5e76\u5728\u7ec4\u4ef6\u5185\u90e8\u4f7f\u7528\u5b83\u3002<\/mark><\/strong>\uff09<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"sharing-data-between-components\">Sharing data between components&nbsp;<a href=\"https:\/\/react.dev\/learn#sharing-data-between-components\"><\/a>\uff08\u5728\u7ec4\u4ef6\u95f4\u4ea4\u6362\u6570\u636e\uff09<\/h2>\n\n\n\n<p>In the previous example, each <code>MyButton<\/code> had its own independent <code>count<\/code>, and when each button was clicked, only the <code>count<\/code> for the button clicked changed:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-56.png\"><img loading=\"lazy\" decoding=\"async\" width=\"557\" height=\"288\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-56.png\" alt=\"\" class=\"wp-image-1207\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-56.png 557w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-56-300x155.png 300w\" sizes=\"(max-width: 557px) 100vw, 557px\" \/><\/a><\/figure>\n\n\n\n<p>However, often you\u2019ll need components to <em>share data and always update together<\/em>.<\/p>\n\n\n\n<p>To make both <code>MyButton<\/code> components display the same <code>count<\/code> and update together, you need to move the state from the individual buttons \u201cupwards\u201d to the closest component containing all of them.\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4e3a\u4e86\u4f7f\u5f97 <code>MyButton<\/code> \u7ec4\u4ef6\u663e\u793a\u76f8\u540c\u7684 <code>count<\/code> \u5e76\u4e00\u8d77\u66f4\u65b0\uff0c\u4f60\u9700\u8981\u5c06\u5404\u4e2a\u6309\u94ae\u7684 state \u201c\u5411\u4e0a\u201d \u79fb\u52a8\u5230\u6700\u63a5\u8fd1\u5305\u542b\u6240\u6709\u6309\u94ae\u7684\u7ec4\u4ef6\u4e4b\u4e2d\u3002<\/mark><\/strong>\uff09<\/p>\n\n\n\n<p>In this example, it is <code>MyApp<\/code> :<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-57.png\"><img loading=\"lazy\" decoding=\"async\" width=\"561\" height=\"673\" src=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-57.png\" alt=\"\" class=\"wp-image-1209\" srcset=\"http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-57.png 561w, http:\/\/codermr.com\/wp-content\/uploads\/2024\/07\/image-57-250x300.png 250w\" sizes=\"(max-width: 561px) 100vw, 561px\" \/><\/a><\/figure>\n\n\n\n<p>Now when you click either button, the <code>count<\/code> in <code>MyApp<\/code> will change, which will change both of the counts in <code>MyButton<\/code>. Here\u2019s how you can express this in code.<\/p>\n\n\n\n<p>First, <em>move the state up<\/em> from <code>MyButton<\/code> into <code>MyApp<\/code> :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default function MyApp() {\n  const &#91;count, setCount] = useState(0);\n\n  function handleClick() {\n    setCount(count + 1);\n  }\n\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Counters that update separately&lt;\/h1&gt;\n      &lt;MyButton \/&gt;\n      &lt;MyButton \/&gt;\n    &lt;\/div&gt;\n  );\n}\n\nfunction MyButton() {\n  \/\/ ... we're moving code from here ...\n}<\/code><\/pre>\n\n\n\n<p>Then, <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><em>pass the state down<\/em> from <code>MyApp<\/code> to each <code>MyButton<\/code><\/mark><\/strong>\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5c06 state \u4ece <\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">MyApp <\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4f20\u9012\u7ed9 <\/mark><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">MyButton<\/mark>\uff09, together with the shared click handler\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u8fde\u540c\u5904\u7406\u51fd\u6570\u4e5f\u4e00\u8d77<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4ece <\/mark><\/strong><\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">MyApp<\/mark> <strong>\u4f20\u9012\u7ed9<\/strong> MyButton<\/mark>\uff09. <br>You can pass information to <code>MyButton<\/code> using the JSX <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">curly braces<\/mark>,\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7\u5411<\/mark><\/strong> <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">MyButton<\/mark> <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4f20\u9012\u4fe1\u606f<\/mark><\/strong>\uff09, just like you previously did with built-in tags like <code>&lt;img&gt;<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default function MyApp() {\n  const &#91;count, setCount] = useState(0);\n\n  function handleClick() {\n    setCount(count + 1);\n  }\n\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Counters that update together&lt;\/h1&gt;\n      {\/* \u6ce8\u610f\uff1a\u4e0b\u9762 {count}\u662f\u5c06 count \u503c\u4f20\u9012\u7ed9 MyButton \u7ec4\u4ef6 *\/}\n      &lt;MyButton count={count} onClick={handleClick} \/&gt;\n      &lt;MyButton count={count} onClick={handleClick} \/&gt;\n    &lt;\/div&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<p>\u2588\u2588<strong>\u6ce8\u610f\u2588\u2588\uff1a<\/strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u4e0a\u9762\u4ee3\u7801 return ( &#8230; ) \u91cc\u9762\u7684 <code>&lt;MyButton count={count} onClick={handleClick} \/&gt;<\/code> \u91cc\u9762\u7684 <code>{count}<\/code>\u662f\u5c06 count \u503c\u4f20\u9012\u7ed9 MyButton \u7ec4\u4ef6\uff0c<code>onClick={handleClick}<\/code> \u662f\u5c06 <code>handleClick<\/code> \u5904\u7406\u51fd\u6570\u4f20\u9012\u7ed9 MyButton \u7ec4\u4ef6\u3002<\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">==\u300bMyButton \u7ec4\u4ef6\u600e\u4e48\u63a5\u53d7\u8fd9\u4e24\u4e2a\u503c\u5462\uff1f\u89c1\u4e0b\u9762\u4ee3\u7801\uff1a<\/mark><\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function MyButton({ count, onClick }) {\n  return (\n    &lt;button onClick={onClick}&gt;\n      Clicked {count} times\n    &lt;\/button&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">The information you pass down like this is called <em>props<\/em><\/mark><\/strong> \uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u4f20\u9012\u7684\u4fe1\u606f\u88ab\u79f0\u4f5c props <\/mark><\/strong>\uff09. Now the <code>MyApp<\/code> component contains the <code>count<\/code> state and the <code>handleClick<\/code> event handler, and <em>passes both of them down as props<\/em> to each of the buttons.\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u6b64\u65f6 <code>MyApp<\/code> \u7ec4\u4ef6\u5305\u542b\u4e86 <code>count<\/code> state \u4ee5\u53ca <code>handleClick<\/code> \u4e8b\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u5e76\u5c06\u5b83\u4eec\u4f5c\u4e3a <strong>prop \u4f20\u9012\u7ed9<\/strong> \u4e86\u6bcf\u4e2a\u6309\u94ae\u3002<\/mark>\uff09<\/p>\n\n\n\n<p style=\"font-size:16px\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-green-cyan-color\">\u4ec0\u4e48\u662f prop \uff1f==\u300b\u4f7f\u7528\u8fd9\u79cd&#8221;\u4ece\u7236\u7ec4\u4ef6\u5f80\u5b50\u7ec4\u4ef6&#8221;\u4f20\u9012\u4fe1\u606f\u7684\u65b9\u5f0f\uff0c\u88ab\u79f0\u4f5c props<\/mark><\/strong><\/p>\n\n\n\n<p>Finally, change <code>MyButton<\/code> to <em>read<\/em> the props you have passed from its parent component\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u6700\u540e\uff0c\u6539\u53d8 <code>MyButton<\/code> \u4ee5 <strong>\u8bfb\u53d6<\/strong> \u4ece\u7236\u7ec4\u4ef6\u4f20\u9012\u6765\u7684 prop\uff1a<\/mark>\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function MyButton({ count, onClick }) {\n  return (\n    &lt;button onClick={onClick}&gt;\n      Clicked {count} times\n    &lt;\/button&gt;\n  );\n}<\/code><\/pre>\n\n\n\n<p>When you click the button, the <code>onClick<\/code> handler <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">fires<\/mark>. Each button\u2019s <code>onClick<\/code> prop was set to the <code>handleClick<\/code> function inside <code>MyApp<\/code>, so the code inside of it runs. That code calls <code>setCount(count + 1)<\/code>, incrementing the <code>count<\/code> state variable. The new <code>count<\/code> value is passed as a prop to each button, so they all show the new value. This is called \u201clifting state up\u201d. By moving state up, you\u2019ve shared it between components.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import { useState } from 'react';\n\nexport default function MyApp() {\n  const &#91;count, setCount] = useState(0);\n\n  function handleClick() {\n    setCount(count + 1);\n  }\n\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Counters that update together&lt;\/h1&gt;\n      &lt;MyButton count={count} onClick={handleClick} \/&gt;\n      &lt;MyButton count={count} onClick={handleClick} \/&gt;\n    &lt;\/div&gt;\n  );\n}\n\nfunction MyButton({ count, onClick }) {\n  return (\n    &lt;button onClick={onClick}&gt;\n      Clicked {count} times\n    &lt;\/button&gt;\n  );\n}\n<\/code><\/pre>\n\n\n\n<p>\u53c2\u8003\u6587\u6863\uff1a<a href=\"https:\/\/react.dev\/learn\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/react.dev\/learn<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This chapter&#8217;s Objectives\uff1a render \u6e32\u67d3\uff1b respond \u54cd\u5e94\uff1b&#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 Basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09 - \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\/react-basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09 - \u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"This chapter&#8217;s Objectives\uff1a render \u6e32\u67d3\uff1b respond \u54cd\u5e94\uff1b...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-27T17:14:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-28T03:29:32+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\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/\"},\"author\":{\"name\":\"\u7801\u5148\u751f\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"headline\":\"React Basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09\",\"datePublished\":\"2024-07-27T17:14:55+00:00\",\"dateModified\":\"2024-07-28T03:29:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/\"},\"wordCount\":1171,\"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\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/\",\"url\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/\",\"name\":\"React Basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09 - \u7801\u5148\u751f\u7684\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/#website\"},\"datePublished\":\"2024-07-27T17:14:55+00:00\",\"dateModified\":\"2024-07-28T03:29:32+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/codermr.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09\"}]},{\"@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 Basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09 - \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\/react-basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09\/","og_locale":"zh_CN","og_type":"article","og_title":"React Basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09 - \u7801\u5148\u751f\u7684\u535a\u5ba2","og_description":"This chapter&#8217;s Objectives\uff1a render \u6e32\u67d3\uff1b respond \u54cd\u5e94\uff1b...","og_url":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09\/","og_site_name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","article_published_time":"2024-07-27T17:14:55+00:00","article_modified_time":"2024-07-28T03:29:32+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\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/#article","isPartOf":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/"},"author":{"name":"\u7801\u5148\u751f","@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"headline":"React Basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09","datePublished":"2024-07-27T17:14:55+00:00","dateModified":"2024-07-28T03:29:32+00:00","mainEntityOfPage":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/"},"wordCount":1171,"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\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/","url":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/","name":"React Basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09 - \u7801\u5148\u751f\u7684\u535a\u5ba2","isPartOf":{"@id":"http:\/\/codermr.com\/#website"},"datePublished":"2024-07-27T17:14:55+00:00","dateModified":"2024-07-28T03:29:32+00:00","breadcrumb":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/react-basics%ef%bc%9a%e8%af%ad%e6%b3%95%e5%9f%ba%e7%a1%80%ef%bc%88%e4%b8%80%ef%bc%89\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/codermr.com\/"},{"@type":"ListItem","position":2,"name":"React Basics\uff1a\u8bed\u6cd5\u57fa\u7840\uff08\u4e00\uff09"}]},{"@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\/1192"}],"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=1192"}],"version-history":[{"count":17,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1192\/revisions"}],"predecessor-version":[{"id":1235,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1192\/revisions\/1235"}],"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=1192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/categories?post=1192"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/tags?post=1192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}