{"id":10008069,"date":"2023-07-10T18:00:10","date_gmt":"2023-07-10T10:00:10","guid":{"rendered":"https:\/\/ityet.com\/?p=10008069"},"modified":"2023-07-10T18:00:10","modified_gmt":"2023-07-10T10:00:10","slug":"%e5%90%ac%e8%af%b4%e5%90%8e%e7%ab%af%e7%9a%84%e4%bd%a0%e5%9c%a8%e5%ad%a6-react","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008069","title":{"rendered":"\u542c\u8bf4\u540e\u7aef\u7684\u4f60\u5728\u5b66 React"},"content":{"rendered":"<h2>\u4e00\u3001React \u662f\u4ec0\u4e48<\/h2>\n<p>\u5728 React \u4e4b\u524d\u524d\u7aef\u6709\u4e09\u4e2a\u91cc\u7a0b\u7891\u610f\u4e49\u7684 library\/framework<\/p>\n<ul>\n<li>jQuery \u89e3\u51b3\u4e86\u6d4f\u89c8\u5668\u517c\u5bb9\u548c DOM \u5143\u7d20\u5feb\u6377\u64cd\u4f5c\u95ee\u9898\uff0c\u5176\u94fe\u5f0f\u64cd\u4f5c API \u4e5f\u5bf9\u540e\u7eed\u524d\u7aef\u6846\u67b6\u4ea7\u751f\u4e86\u6df1\u523b\u5f71\u54cd\uff1b<\/li>\n<li>Knockout \u63d0\u51fa\u4e86\u524d\u7aef\u4ee3\u7801 MVVM \u5206\u5c42\u7406\u5ff5\uff0c\u6570\u636e\u901a\u8fc7\u6a21\u677f\u6620\u5c04\u4e3a UI \u89c6\u56fe\uff0c\u5927\u5e45\u5ea6\u51cf\u5c11\u4e86 DOM \u64cd\u4f5c\uff1b<\/li>\n<li>AngularJS \u5728 MVVM \u57fa\u7840\u4e0a\u5f15\u5165\u4e86\u53cc\u5411\u7ed1\u5b9a\uff0c\u6570\u636e\u53d8\u5316\u81ea\u52a8\u53cd\u6620\u5230 UI\uff0c\u89c6\u56fe\u4e0a\u7684\u64cd\u4f5c\u4e5f\u53cd\u5411\u81ea\u52a8\u66f4\u65b0\u6570\u636e\uff1b\u5176\u901a\u8fc7\u6307\u4ee4\u62d3\u5c55 HTML \u7684\u98ce\u683c\u63d0\u5347\u4e86\u6a21\u677f\u5f15\u64ce\u7684\u7075\u6d3b\u6027\uff0c\u53ef\u60dc\u4f5c\u8005\u5f15\u5165\u4e86\u5927\u91cf\u501f\u9274\u670d\u52a1\u5668\u7f16\u7a0b\u7684\u6982\u5ff5\uff0c\u8ba9 AugularJS \u5b66\u4e60\u6210\u672c\u76f4\u7ebf\u4e0a\u5347\uff0c\u6027\u80fd\u4e5f\u7565\u6709\u4e0d\u8db3\uff1b<\/li>\n<\/ul>\n<p>React \u662f\u4e00\u4e2a\u58f0\u660e\u5f0f\u3001\u9ad8\u6548\u3001\u7075\u6d3b\u7684\u7528\u4e8e\u6784\u5efa\u7528\u6237\u754c\u9762\u7684 JavaScript library\uff0cReact \u6838\u5fc3\u7406\u5ff5\u5728\u4e8e\u5c06\u4e00\u4e9b\u7b80\u77ed\u3001\u72ec\u7acb\u7684\u4ee3\u7801\u7247\u6bb5\u7ec4\u5408\u6210\u590d\u6742\u7684 UI \u754c\u9762\uff0c\u8fd9\u4e9b\u4ee3\u7801\u7247\u6bb5\u88ab\u79f0\u4f5c \u201cComponent\u201d\u3002React \u4e0d\u662f MVC \u6846\u67b6\uff0c\u66f4\u50cf\u662f\u5176\u4e2d V\uff0c\u4ec5\u4ec5\u8d1f\u8d23\u7528\u6237\u4ea4\u4e92\u89c6\u56fe\u7684\u6e32\u67d3\u3002<br \/>React \u5e26\u6765\u4e86\u4e09\u4e2a\u98a0\u8986\u6027\u7406\u5ff5\uff0c\u5728\u63a5\u4e0b\u6765\u7684\u7ae0\u8282\u4e2d\u5c06\u4e00\u4e00\u4ecb\u7ecd\uff1a<\/p>\n<ul>\n<li>JSX\uff0c\u4f7f\u7528 JavaScript \u8868\u8fbe UI + \u4ea4\u4e92\uff0c\u5145\u5206\u5229\u7528 JavaScript \u7684\u7075\u6d3b\u6027\uff1b<\/li>\n<li>fx(props) = UI\uff0c\u6570\u636e\u9a71\u52a8 UI\uff0c\u5355\u5411\u6570\u636e\u6d41\u3001\u51fd\u6570\u98ce\u683c\u7684\u9875\u9762\u7ec4\u4ef6\uff1b<\/li>\n<li>Virtual DOM\uff0c\u670d\u52a1\u5668\u3001\u5ba2\u6237\u7aef\u4f7f\u7528\u540c\u4e00\u5957\u4ee3\u7801\u6e32\u67d3\u2014\u2014\u540c\u6784\uff0c\u89e3\u51b3\u524d\u7aef\u5e94\u7528 SEO \u95ee\u9898\uff1b<\/li>\n<\/ul>\n<h2>\u4e8c\u3001\u5feb\u901f\u521d\u59cb\u5316 React \u9879\u76ee<\/h2>\n<p>\u4f7f\u7528 Create React App [1]\u53ef\u4ee5\u5feb\u901f\u521d\u59cb\u5316\u4e00\u4e2a React Web \u9879\u76ee\u3002<\/p>\n<pre><code>$ npx create-react-app learn-react --template typescript$ cd learn-react$ npm start<\/code><\/pre>\n<p>\u6267\u884c npm start\u540e\u6d4f\u89c8\u5668\u4f1a\u5728 <a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=tRnKu%2BVAGVj6fDzMTaGNAA%3D%3D.PfX8RtMQuzYhZu6UrcppEFMU6kuuI7BkO%2BtAYbf%2BSsE%3D\" rel=\"noopener noreferrer\">http:\/\/localhost:3000<\/a> \u6253\u5f00\u9879\u76ee\u9996\u9875\u3002<\/p>\n<h2>\u4e09\u3001\u8c03\u8bd5 React \u5e94\u7528<\/h2>\n<p>React \u63d0\u4f9b\u4e86 React Developer Tools[2]\uff0c\u96c6\u6210\u5230\u4e86 Chrome Dev Tools\uff0c\u501f\u6b64\u53ef\u4ee5\u67e5\u770b React \u7ec4\u4ef6\u6811\u53ca\u5176\u5bf9\u5e94 Props\u3001State\u3002app.tsx<\/p>\n<pre><code>import React, { useState } from 'react';\nfunction Button(props: { count: number }): JSX.Element {  const [count, setCount] = useState(props.count);  return (    &lt;button      onClick={() =&gt; {        setCount((c) =&gt; c + 1);      }}      &gt;      {count}    &lt;\/button&gt;  );}\nfunction App() {  const [count, setCount] = useState(0);\n  return (    &lt;div className=&quot;App&quot;&gt;      &lt;Button count={5} \/&gt;    &lt;\/div&gt;  );}\nexport default App;<\/code><\/pre>\n<p>index.tsx<\/p>\n<pre><code>import React from 'react';import * as ReactDOMClient from 'react-dom\/client';\nimport App from '.\/app';\nconst rootElement = document.querySelector('body') as Element;const root = ReactDOMClient.createRoot(rootElement);\nroot.render(&lt;App \/&gt;);<\/code><\/pre>\n<p>\u6253\u5f00 Chrome Dev Tools \u53ef\u4ee5\u770b\u5230\u591a\u4e86\u4e00\u4e2a Components\u9009\u9879\u5361<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043992575\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\"><\/p>\n<h2>\u56db\u3001Todo project<\/h2>\n<p>\u63a5\u4e0b\u6765\u8fb9\u5b66\u4e60\u8fb9\u505a\u4e00\u4e2a Todo \u9879\u76ee\u4f53\u9a8c\u4e00\u4e0b React\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043992576\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<h2>\u4e94\u3001\u4f7f\u7528 JSX \u505a\u66f4\u597d\u7684\u5173\u6ce8\u70b9\u5206\u79bb<\/h2>\n<p>\u5728\u5f00\u59cb\u7f16\u5199 React \u7a0b\u5e8f\u4e4b\u524d\u9700\u8981\u4e86\u89e3\u4e00\u4e0b JSX\u3002JSX \u662f React \u5bf9 JavaScript \u7684\u8bed\u6cd5\u62d3\u5c55\uff0c\u7528\u6765\u5728 JavaScript \u6587\u4ef6\u5185\u901a\u8fc7\u7c7bHTML\u6807\u7b7e\uff08HTML-like markup\uff09\u8868\u8fbe\u9875\u9762\u7684\u89c6\u56fe\u4e0e\u4ea4\u4e92\u903b\u8f91\u3002<\/p>\n<pre><code>&lt;div className=&quot;container&quot;&gt;  &lt;CustomComponent     onClick={() =&gt; {alert('Hello')}}  &gt;    Hello {props.name}!  &lt;\/CustomComponent&gt;&lt;\/div&gt;<\/code><\/pre>\n<p>Web \u9875\u9762\u7531 HTML \u5185\u5bb9\u3001CSS \u6837\u5f0f\u3001JavaScript \u4ea4\u4e92\u6784\u6210\uff0c\u957f\u671f\u4ee5\u6765 Web \u5f00\u53d1\u8005\u5c06\u4e09\u8005\u653e\u5728\u72ec\u7acb\u7684\u6587\u4ef6\u4e2d\u505a\u5206\u79bb\uff0c\u8fd9\u5b9e\u9645\u4e0a\u662f\u6309\u7167\u6280\u672f\u5b9e\u73b0\u7684\u5206\u79bb\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043992577\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u4f20\u7edf\u9875\u9762\u5185\u5bb9\u4e3b\u8981\u7531 HTML \u5b9a\u4e49\uff0cJavaScript \u903b\u8f91\u662f\u70b9\u7f00\uff0c\u968f\u7740\u73b0\u4ee3\u7f51\u9875\u4ea4\u4e92\u6027\u589e\u5f3a\uff0c\u9875\u9762\u5185\u5bb9\u5f88\u5927\u7a0b\u5ea6\u662f\u7531 JavaScript \u903b\u8f91\u52a8\u6001\u751f\u6210\uff0c\u540c\u65f6\u6e32\u67d3\u903b\u8f91\u672c\u8d28\u4e0a\u4e0e\u5176\u4ed6 UI \u903b\u8f91\u5185\u5728\u8026\u5408\uff0c\u6bd4\u5982\u5728 UI \u4e2d\u9700\u8981\u7ed1\u5b9a\u5904\u7406\u4e8b\u4ef6\u3001\u5728\u67d0\u4e9b\u65f6\u523b\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\u9700\u8981\u901a\u77e5\u5230 UI\uff0c\u4ee5\u53ca\u9700\u8981\u5728 UI \u4e2d\u5c55\u793a\u51c6\u5907\u597d\u7684\u6570\u636e\u3002<\/p>\n<p>\u56e0\u6b64 React \u4f7f\u7528 JSX \u628a\u6e32\u67d3\u903b\u8f91\u548c HTML \u6807\u7b7e\u96c6\u6210\u5230\u4e00\u8d77\u3002<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043992578\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u6837\u5f00\u53d1\u8005\u5173\u6ce8\u7684\u4e0d\u662f HTML \u6a21\u677f\u3001JavaScript \u6e32\u67d3\u903b\u8f91\u8fd9\u6837\u7684\u6280\u672f\u5b9e\u73b0\uff0c\u800c\u662f\u8bf8\u5982 Sidebar\u3001Form \u8fd9\u6837\u7684\u9875\u9762\u529f\u80fd\u5355\u5143\u3002<\/p>\n<h2>\u516d\u3001\u4f7f\u7528 JSX \u7f16\u5199 React \u7ec4\u4ef6<\/h2>\n<p>\u8fd4\u56de JSX \u7684\u51fd\u6570\u5c31\u662f React \u6700\u7b80\u5355\u7684\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u548c HTML \u6807\u7b7e\u4e00\u6837\u5d4c\u5957\u4f7f\u7528\u3002React \u4f7f\u7528 props\u53c2\u6570\u5411\u7ec4\u4ef6\u4f20\u9012\u6570\u636e\uff0c\u63d0\u5347\u7ec4\u4ef6\u7684\u590d\u7528\u6027\u3002<\/p>\n<pre><code>\/** * JSX \u8bed\u6cd5\u9690\u5f0f\u8c03\u7528 React.createElement * \u6240\u4ee5\u867d\u7136\u4ee3\u7801\u4e2d\u6ca1\u6709\u8c03\u7528 React \u7684\u8bed\u53e5\uff0c\u4ecd\u7136\u9700\u8981\u5f15\u5165  *\/import React from 'react'; \ninterface IButton {  \/** \u6309\u94ae\u5c55\u793a\u6587\u6848 *\/  text: string;  \/** \u70b9\u51fb\u6309\u94ae\u8df3\u8f6c\u94fe\u63a5 *\/  link?: string;  \/** \u70b9\u51fb\u6309\u94ae\u81ea\u5b9a\u4e49\u4e8b\u4ef6 *\/  onClick?: (event?: Event) =&gt; void}\nfunction Button(props: IButton) {  const { text, link, onClick } = props;\n  const redirectHandler = () =&gt; {    location.href = link;  };\n  return (    &lt;div      className=&quot;button&quot;      onClick={onClick | redirectHandler}    &gt;      {text}    &lt;\/div&gt;  );}\nexport default Button;<\/code><\/pre>\n<p>\u5728\u4f7f\u7528\u7ec4\u4ef6\u65f6\u5019\uff0c\u901a\u8fc7\u5176\u6807\u7b7e\u7684\u5c5e\u6027\u7ec4\u88c5\u6210 props \u5bf9\u8c61\uff0c\u4f20\u9012\u7ed9\u7ec4\u4ef6\uff0c\u8bed\u6cd5\u548c HTML attribute \u7c7b\u4f3c\uff0c\u4f46\u503c\u53ef\u4ee5\u662f\u4efb\u610f\u7684 JavaScript \u5bf9\u8c61\u3002<\/p>\n<pre><code>import React from 'react';\n\/** * \u5bfc\u5165 .\/button.tsx \u4e2d export \u7684\u9ed8\u8ba4\u5185\u5bb9\uff0c\u547d\u540d\u4e3a Button \u4f7f\u7528 * .tsx \u62d3\u5c55\u540d\u53ef\u4ee5\u7701\u7565 *\/import Button from '.\/button';\ninterface IDialog {  title: string;  content: Element;  showClose: boolean;}\nfunction Dialog(props: IDialog) {  const { title, content, showClose = false, children } = props;\n  const hideDialog = () =&gt; {    \/\/ ...  }\n  return (    &lt;div&gt;      &lt;div className=&quot;dialog-title&quot;&gt; {title} &lt;\/div&gt;      &lt;div className=&quot;dialog-body&quot;&gt; {content | children} &lt;\/div&gt;      {\/* \u6ca1\u9519\uff0cButton props \u5b9a\u4e49\u7684\u5c5e\u6027\uff0c\u5c31\u662f\u8fd9\u6837\u901a\u8fc7\u6807\u7b7e\u5c5e\u6027\u5f00\u653e\u51fa\u6765\u7684 *\/}      &lt;Button        title=&quot;\u53d6\u6d88&quot;        onClick={hideDialog}      \/&gt;      &lt;Button         title=&quot;\u786e\u8ba4&quot;        onClick={() =&gt; { }}      \/&gt;    &lt;\/div&gt;  );}\nexport default Dialog;<\/code><\/pre>\n<p>\u7ec4\u4ef6\u5199\u597d\u540e\u901a\u8fc7 react-dom [3]\u5c06\u7ec4\u4ef6\u6e32\u67d3\u5230\u9875\u9762\u3002<\/p>\n<pre><code>import React from 'react';import ReactDOM from 'react-dom\/client';import Dialog from '.\/dialog';\n\/\/ \u628a\u7ec4\u4ef6\u6e32\u67d3\u5230\u9875\u9762 id \u4e3a root \u7684\u5143\u7d20\u4e2dconst rootElement = document.getElementById('root');const root = ReactDOM.createRoot(rootElement);root.render(  &lt;Dialog     title=&quot;demo dialog&quot;     content=&quot;this is a dialog&quot;    showClose={false}  \/&gt;);<\/code><\/pre>\n<h2>\u4e03\u3001JSX \u89c4\u5219<\/h2>\n<p>React \u7ec4\u4ef6\u6709\u51e0\u4e2a\u7ea6\u5b9a\uff1a<\/p>\n<ul>\n<li>\u7ec4\u4ef6\u540d\u79f0\u4f7f\u7528 Pascal \u98ce\u683c\uff08\u9996\u5b57\u6bcd\u5927\u5199\uff09\uff0c\u4ee5\u548c HTML \u539f\u751f\u6807\u7b7e\uff08div\u3001p\u3001a \u7b49\uff09\u533a\u5206\uff1b<\/li>\n<li>\u7ec4\u4ef6\u4ec5\u63a5\u53d7 props\u4e00\u4e2a\u53c2\u6570\uff0c\u7528\u6765\u66b4\u9732\u7ec4\u4ef6\u53ef\u914d\u7f6e\u5c5e\u6027\uff0c\u5176\u5b50\u7ec4\u4ef6\u88ab React \u901a\u8fc7 children\u5c5e\u6027\u6ce8\u5165\uff1b<\/li>\n<li>\u5728\u7ec4\u4ef6\u5185\u90e8 props \u662f\u53ea\u8bfb\u7684\uff0c\u4e0d\u5141\u8bb8\u5bf9\u5176\u8fdb\u884c\u4fee\u6539\uff1b<\/li>\n<\/ul>\n<h3>1.\u5fc5\u987b\u6709\u6839\u8282\u70b9<\/h3>\n<p>\u5982\u540c\u4e0a\u9762\u5199\u7684\u51e0\u4e2a\u7b80\u5355 demo\uff0cJSX \u5fc5\u987b\u6709 root \u8282\u70b9\uff0c\u5373\u4f7f\u591a\u4e2a\u540c\u7ea7\u5143\u7d20\u6ca1\u6709\u7236\u8282\u70b9\uff0c\u4e5f\u9700\u8981\u7528\u865a\u62df\u8282\u70b9 &lt;&gt; \u6765\u5305\u88f9\u3002<\/p>\n<pre><code>{\/* \u975e\u6cd5\u7684 JSX *\/}&lt;div id=&quot;box1&quot;&gt;&lt;\/div&gt;&lt;div id=&quot;box2&quot;&gt;&lt;\/div&gt;<\/code><\/pre>\n<pre><code>{\/* \u5408\u6cd5\u7684 JSX *\/}&lt;&gt;    &lt;div id=&quot;box1&quot;&gt;&lt;\/div&gt;  &lt;div id=&quot;box2&quot;&gt;&lt;\/div&gt;&lt;\/&gt;<\/code><\/pre>\n<h3>2.\u6240\u6709\u6807\u7b7e\u9700\u8981\u95ed\u5408<\/h3>\n<p>\u5728 HTML \u4e2d\u6807\u7b7e\u5e76\u4e0d\u4e00\u5b9a\u9700\u8981\u95ed\u5408\u3002<\/p>\n<pre><code>&lt;meta charset=&quot;UTF-8&quot;&gt;&lt;br&gt;&lt;img src=&quot;https:\/\/g.alicdn.com\/logo.png&quot;&gt;<\/code><\/pre>\n<p>\u5728 JSX \u4e2d\u53ef\u4ee5\u6df7\u5408 HTML \u539f\u751f\u6807\u7b7e\uff0c\u4f46\u6240\u6709\u6807\u7b7e\u5fc5\u987b\u95ed\u5408\u3002<\/p>\n<pre><code>&lt;&gt;  &lt;meta charset=&quot;UTF-8&quot; \/&gt;  &lt;br\/&gt;  &lt;img src=&quot;https:\/\/g.alicdn.com\/logo.png&quot;\/&gt;&lt;\/&gt;<\/code><\/pre>\n<h3>3.\u548c HTML \u5c5e\u6027\u5dee\u5f02<\/h3>\n<ul>\n<li>\u5728 React \u4e2d\u5e38\u7528\u7684 DOM \u7279\u6027\u548c\u5c5e\u6027\uff08\u5305\u62ec\u4e8b\u4ef6\u5904\u7406\uff09\u90fd\u4f7f\u7528\u5c0f\u9a7c\u5cf0\u547d\u540d\u7684\u65b9\u5f0f\uff0c\u4f8b\u5982\u4e0e HTML \u4e2d\u7684 tabindex \u5c5e\u6027\u5bf9\u5e94\u7684 React \u7684\u5c5e\u6027\u662f tabIndex\uff1b<\/li>\n<li>HTML \u90e8\u5206\u5c5e\u6027\u540d\u79f0\u4e0e JavaScript \u4fdd\u7559\u5b57\u51b2\u7a81\uff0c\u5728 JSX \u4e2d\u9700\u8981\u4f7f\u7528\u66ff\u4ee3\u540d\u79f0\uff1b<\/li>\n<\/ul>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043992579\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<ul>\n<li>style \u5c5e\u6027 value \u662f\u4e00\u4e2a CSS \u5c5e\u6027\u7ec4\u6210\u7684\u5bf9\u8c61\uff0c\u4e3a\u4e86\u8ba9\u5176\u7b26\u5408 JavaScript \u8bed\u6cd5\u89c4\u5219\uff0c\u5c5e\u6027\u540d\u4f7f\u7528\u9a7c\u5cf0\u547d\u540d\uff08fontSize\u3001backgroundColor\uff09\uff0c\u800c\u4e0d\u662f CSS \u5c5e\u6027\u4f7f\u7528\u7684\u8fde\u5b57\u7b26\uff0c\u8fd9\u6837\u53ef\u4ee5\u5f88\u65b9\u4fbf\u8bbe\u7f6e\u52a8\u6001\u6837\u5f0f\uff0c\u4f46\u9759\u6001\u6837\u5f0f\u5e94\u8be5\u4f9d\u8d56 className \u548c CSS \u6587\u4ef6\u7684\u914d\u5408\uff1b<\/li>\n<\/ul>\n<pre><code>function HelloWorldComponent(props) {    const divStyle = {      \/\/ \u53ef\u4ee5\u5f88\u65b9\u4fbf\u8bbe\u7f6e\u52a8\u6001\u6837\u5f0f      backgroundImage: 'url(' + props.imgUrl + ')',    \/\/ \u4f46\u9759\u6001\u6837\u5f0f\u5e94\u8be5\u5c3d\u91cf\u901a\u8fc7 className \u8bbe\u7f6e\u7c7b\uff0c\u901a\u8fc7 css file \u89e3\u51b3    \/\/ \u4e0d\u63a8\u8350 color: 'blue' \u8fd9\u79cd\u9759\u6001\u6837\u5f0f\u76f4\u63a5\u5728 JSX \u7684\u5199\u6cd5    color: 'blue',  };\n  return (    &lt;div style={divStyle}&gt;      Hello World!    &lt;\/div&gt;  );}<\/code><\/pre>\n<ul>\n<li>React \u5bf9\u4e8e Form \u8868\u5355\u652f\u6301 defaultValue \u5c5e\u6027\uff0c\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c\u5728\u8fd0\u884c\u65f6\u53d6\u503c\u4f7f\u7528\u548c HTML \u4e00\u81f4\u7684 value\u5c5e\u6027\u3002<\/li>\n<\/ul>\n<h3>4.\u81ea\u52a8\u8f6c\u4e49 content<\/h3>\n<p>\u4e3a\u4e86\u9632\u6b62 XSS \u653b\u51fb\uff0cJSX \u4f1a\u5bf9\u76f4\u63a5\u8bbe\u7f6e\u7684\u6587\u672c\u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n<pre><code>const content = `  \u8fd9\u91cc\u5e94\u8be5\u5c55\u793a\u4e00\u5f20\u56fe\u7247&lt;br&gt;  &lt;img src=&quot;https:\/\/sc02.alicdn.com\/kf\/HTB1gUuPUkzoK1RjSZFl761i4VXaw.png&quot; \/&gt;`;&lt;div&gt;  {content}&lt;\/div&gt;<\/code><\/pre>\n<p>\u9875\u9762\u6548\u679c\uff1a<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043992580\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<p>\u5728\u5b89\u5168\u6027\u6709\u4fdd\u969c\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u901a\u8fc7 dangerouslySetInnerHTML\u7981\u7528\u8f6c\u4e49\u6548\u679c\uff0c\u5c55\u793a raw HTML<\/p>\n<pre><code>const content = `      \u8fd9\u91cc\u5e94\u8be5\u5c55\u793a\u4e00\u5f20\u56fe\u7247&lt;br&gt;      &lt;img src=&quot;https:\/\/sc02.alicdn.com\/kf\/HTB1gUuPUkzoK1RjSZFl761i4VXaw.png&quot; \/&gt;  `;&lt;div dangerouslySetInnerHTML={{ __html: content }}\/&gt;<\/code><\/pre>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043992581\" alt=\"\u56fe\u7247\" title=\"\u56fe\u7247\" loading=\"lazy\"><\/p>\n<h2>\u516b\u3001\u5728 JSX \u4e2dTODO\u4f7f\u7528 {} \u652f\u6301 JavaScript \u8868\u8fbe\u5f0f<\/h2>\n<p>JSX \u4e2d\u4f7f\u7528 {} \u5305\u88f9 JavaScript \u8868\u8fbe\u5f0f\u5904\u7406\u52a8\u6001\u903b\u8f91\uff0c\u5c5e\u6027 value\u3001\u5b50\u5143\u7d20\u90fd\u53ef\u4ee5\uff0c\u6700\u5e38\u89c1\u7684\u51e0\u4e2a\u7528\u6cd5\uff1a<\/p>\n<ul>\n<li>{\u53d8\u91cf\u540d}\u8bfb\u53d6\u53d8\u91cf\u503c\uff0c\u53cc\u5c42 {{}} \u5e76\u4e0d\u662f\u7279\u6b8a\u8bed\u6cd5\uff0c\u800c\u662f {\u5bf9\u8c61} \u7684\u5feb\u6377\u5199\u6cd5<\/li>\n<\/ul>\n<pre><code>&lt;div style={{ color: 'red' }}&gt;&lt;\/div&gt;\n\/\/ \u7b49\u540c\u4e8e\nconst styleObj = { color: 'red' };&lt;div style={styleObj}&gt;&lt;\/div&gt;<\/code><\/pre>\n<ul>\n<li>\u4e09\u5143\u8868\u8fbe\u5f0f\u5904\u7406 if-else\uff08if-else \u662f\u8bed\u53e5\uff0c\u4e0d\u662f\u8868\u8fbe\u5f0f\uff09<\/li>\n<li>map \u5904\u7406\u5faa\u73af\u903b\u8f91\uff0c\u6279\u91cf\u751f\u6210\u5143\u7d20<\/li>\n<\/ul>\n<pre><code>interface IStuff {  name: string;  sex: 'male' | 'female';} function App () {  const list: Array&lt;IStuff&gt; = [    { name: 'Byron', sex: 'male' },    { name: 'Casper', sex: 'male' },    { name: 'Junice', sex: 'female' },  ];   return (    &lt;ul className=&quot;stuff-list&quot;&gt;      {        list.map(stuff =&gt; { \/\/ \u751f\u6210\u591a\u4e2a          const { name, sex } = stuff;          return (            {            &lt;li              \/* \u5b9e\u9645\u7f16\u7a0b className \u8bbe\u7f6e\u6709\u66f4\u597d\u7684\u8868\u8fbe\u65b9\u5f0f\uff0c\u8fd9\u91cc\u4ec5 demo \u4e09\u5143\u8868\u8fbe\u5f0f\u4f7f\u7528 *\/}                className={sex === 'male' ? 'stuff-male' : 'stuff-female'}              onClick={() =&gt; { alert(name) }}            &gt;              \/\/ \u8bfb\u53d6\u53d8\u91cf\u503c              {name}            &lt;\/li&gt;          );        })      }    &lt;\/ul&gt;  );}<\/code><\/pre>\n<p>JSX \u4e2d\u6ce8\u91ca\u4e5f\u9700\u8981\u4f7f\u7528 {} \u5305\u88f9\uff0c\u4f46\u8fd9\u79cd\u5199\u6cd5\u8fc7\u4e8e\u4e0d\u65b9\u4fbf\uff0c\u5927\u90e8\u5206\u7f16\u8bd1\u5de5\u5177\u90fd\u53ef\u4ee5\u5904\u7406\u53cc\u659c\u7ebf\u98ce\u683c\/\/\u6ce8\u91ca<\/p>\n<h2>\u4e5d\u3001JSX \u7684\u80cc\u540e<\/h2>\n<p>JSX \u7684\u8fd4\u56de\u503c\u65e2\u4e0d\u662f DOM \u5143\u7d20\uff0c\u4e5f\u4e0d\u662f HTML \u5b57\u7b26\u4e32\uff0c\u800c\u662f\u5bf9 DOM \u7684\u4e00\u4e2a JSON \u63cf\u8ff0\uff0c\u8fd9\u5c31\u662f React Element\uff1a<\/p>\n<pre><code>&lt;button id=&quot;9527&quot; className=&quot;btn-primary&quot;&gt;   &lt;span style={{ color: 'red' }}&gt;     This is a Button   &lt;\/span&gt;&lt;\/button&gt;<\/code><\/pre>\n<p>JSX \u7528\u7c7b\u4f3c\u8fd9\u6837\u7684\u7ed3\u6784\u8868\u8fbe\uff1a<\/p>\n<pre><code>{  &quot;type&quot;: &quot;button&quot;,  &quot;props&quot;: {    &quot;id&quot;: &quot;9527&quot;,    &quot;className&quot;: &quot;btn-primary&quot;,    &quot;children&quot;: [      {        &quot;type&quot;: &quot;span&quot;,        &quot;props&quot;: {          &quot;style&quot;: { &quot;color&quot;: &quot;red&quot; },          &quot;children&quot;: &quot;This is a Button&quot;        }      }    ]  }}<\/code><\/pre>\n<p>\u7f16\u8bd1\u540e\u5b9e\u9645\u662f\u8fd9\u6837\u7684\u8c03\u7528\uff1a<\/p>\n<pre><code>React.createElement(&quot;button&quot;, {  id: &quot;9527&quot;,  className: &quot;btn-primary&quot;},React.createElement(&quot;span&quot;, {  style: {    color: 'red'  }}, &quot;This is a Button&quot;));<\/code><\/pre>\n<p>React.createElement(type, props, &#8230;children)\uff0c\u4e0a\u6587\u63d0\u5230\u8fc7 React \u4f1a\u81ea\u52a8\u628a children \u6ce8\u5165\u5230 props\uff0c\u5c31\u662f\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u3002<\/p>\n<p>\u4e86\u89e3\u4e86 JSX \u4e4b\u540e\u53ef\u4ee5\u5f00\u59cb\u7f16\u5199\u9759\u6001\u7684 React Component \u4e86\u3002\u5b8c\u6574\u6559\u7a0b\u89c1\u8bed\u96c0\uff1a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=4iG1YEjb1VwG9ts%2BFanOSA%3D%3D.r7gsgglMzqTZBRisdTc5StOCSf35uCQjk1zsKsJ9eB6Cu43WRj9ER2M9ZJvnsvFoIECVX6sUKHnw4ukurP1bIg%3D%3D\" rel=\"noopener noreferrer\">https:\/\/www.yuque.com\/sunluyong\/fe4java\/pwsehvspthh6gtrd<\/a><\/p>\n<p><strong>\u53c2\u8003\u94fe\u63a5\uff1a<\/strong><\/p>\n<p>[1]<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=AkI9TRIwmZyvDeIZcpnVLQ%3D%3D.Kw30BrmR3hpVoEAuRmS8LmUKHC3nGdddUChNqsQKjm0%3D\" rel=\"noopener noreferrer\">https:\/\/create-react-app.dev\/<\/a><\/p>\n<p>[2]<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=sLQuN%2BijNJnVGE4HCjWs9Q%3D%3D.IU7Irww3mHNtVSiVqKCmecVglW2vdzGcTrNV47Q1M8yiBpIjxicfw3hTEv9S7ZOYptnoq1bPeNuDdhe9NedEiOelThKo8g%2FMCaD3Rq%2FT4M1IEryZVJIW5C8Ibp5WpfmXK%2BTR3FJVTOYYl4EDMM9rmQ%3D%3D\" rel=\"noopener noreferrer\">https:\/\/chrome.google.com\/webstore\/detail\/react-developer-too&#8230;<\/a><\/p>\n<p>[3]<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=XChsblgqR5JN%2FfcPM7hddg%3D%3D.OodHiHkRTBlJAuU94emtXw3tmbSaxcCn9hOH5uKBH3sox0g3aIuWiHarXJHJCZDF\" rel=\"noopener noreferrer\">https:\/\/www.npmjs.com\/package\/react-dom<\/a><\/p>\n<p>\u4f5c\u8005\uff5c\u8c26\u884c<\/p>\n<p><strong><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=6U%2B00dv5KC9FMVAT09eE3Q%3D%3D.V2ggIo8xNsm2TrE0S3OVc%2FMT6Udt5lR3PnFUU7d01KGFYMX5qtsl27i4yAOH3txD\" rel=\"noopener noreferrer\">\u70b9\u51fb\u7acb\u5373\u514d\u8d39\u8bd5\u7528\u4e91\u4ea7\u54c1 \u5f00\u542f\u4e91\u4e0a\u5b9e\u8df5\u4e4b\u65c5\uff01<\/a><\/strong><\/p>\n<p><strong><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=n8mqKc6AWYvA%2BNJ%2BgdF5Xw%3D%3D.He3MQZ98P4inIwLjsa1CMoIUa5t7ovj6hfrUNSVCATM4hZBq2tH1bVl5IAojtyc8\" rel=\"noopener noreferrer\">\u539f\u6587\u94fe\u63a5<\/a><\/strong><\/p>\n<p><strong>\u672c\u6587\u4e3a\u963f\u91cc\u4e91\u539f\u521b\u5185\u5bb9\uff0c\u672a\u7ecf\u5141\u8bb8\u4e0d\u5f97\u8f6c\u8f7d\u3002<\/strong><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043992573\">https:\/\/segmentfault.com\/a\/1190000043992573<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001React \u662f\u4ec0\u4e48 \u5728 React \u4e4b\u524d\u524d\u7aef\u6709\u4e09\u4e2a\u91cc\u7a0b\u7891\u610f\u4e49\u7684 library\/framework jQu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008069","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008069","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008069"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008069\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}