{"id":1254,"date":"2024-07-28T18:57:38","date_gmt":"2024-07-28T10:57:38","guid":{"rendered":"http:\/\/codermr.com\/?p=1254"},"modified":"2024-07-28T18:58:00","modified_gmt":"2024-07-28T10:58:00","slug":"render-and-commit","status":"publish","type":"post","link":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/","title":{"rendered":"Render and Commit"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\u6e32\u67d3\u548c\u63d0\u4ea4<\/h1>\n\n\n\n<p>Before your components are displayed on screen, they must be rendered by React. Understanding the steps in this process will help you think about how your code executes and explain its behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">You will learn<\/h3>\n\n\n\n<ul>\n<li>What rendering means in React\uff08<strong>\u5728 React \u4e2d\uff0c&#8221;\u6e32\u67d3&#8221;\u662f\u4ec0\u4e48\u610f\u601d<\/strong>\uff09<\/li>\n\n\n\n<li>When and why React renders a component<strong>\uff08\u4e3a\u4ec0\u4e48\u4ee5\u53ca\u4ec0\u4e48\u65f6\u5019 React \u4f1a\u6e32\u67d3\u4e00\u4e2a\u7ec4\u4ef6\uff09<\/strong><\/li>\n\n\n\n<li>The steps involved in displaying a component on screen<strong>\uff08\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u7ec4\u4ef6\u6240\u5305\u542b\u7684\u6b65\u9aa4\uff09<\/strong><\/li>\n\n\n\n<li>Why rendering does not always produce a DOM update<strong>\uff08\u4e3a\u4ec0\u4e48\u6e32\u67d3\u5e76\u4e0d\u4e00\u5b9a\u4f1a\u5bfc\u81f4 DOM \u66f4\u65b0\uff09<\/strong><\/li>\n<\/ul>\n\n\n\n<p>Imagine that <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">your components are cooks in the kitchen<\/mark><\/strong>, <strong><em>assembling <\/em><\/strong>tasty dishes from <strong><em>ingredients<\/em><\/strong>\uff08\u60f3\u8c61\u4e00\u4e0b<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u4f60\u7684\u7ec4\u4ef6\u662f\u53a8\u623f\u91cc\u7684\u53a8\u5e08<\/mark><\/strong>\uff0c\u628a\u98df\u6750\u70f9\u5236\u6210\u7f8e\u5473\u7684\u83dc\u80b4\u3002\uff09.<br> In this <strong><em>scenario<\/em><\/strong>, <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">React is the waiter who <strong><em>puts in requests<\/em><\/strong> from customers and brings them their orders\uff08<strong>React \u5c31\u662f\u4e00\u540d\u670d\u52a1\u5458\uff0c\u4ed6\u4f1a\u5e2e\u5ba2\u6237\u4eec\u4e0b\u5355\u5e76\u4e3a\u4ed6\u4eec\u9001\u6765\u6240\u70b9\u7684\u83dc\u54c1\u3002<\/strong>\uff09<\/mark>. This process of requesting and serving UI has three steps:<\/p>\n\n\n\n<ol>\n<li><strong>Triggering<\/strong>\u00a0a render (delivering the guest\u2019s order to the kitchen)\u3010<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u89e6\u53d1<\/strong>\u00a0\u4e00\u6b21\u6e32\u67d3\uff08\u628a\u5ba2\u4eba\u7684\u70b9\u5355\u5206\u53d1\u5230\u53a8\u623f\uff09<\/mark>\u3011<\/li>\n\n\n\n<li><strong>Rendering<\/strong>\u00a0the component (preparing the order in the kitchen)<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u3010<strong>\u6e32\u67d3<\/strong>\u00a0\u7ec4\u4ef6\uff08\u5728\u53a8\u623f\u51c6\u5907\u8ba2\u5355\uff09\u3011<\/mark><\/li>\n\n\n\n<li><strong>Committing<\/strong>\u00a0to the DOM (placing the order on the table)<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u3010<strong>\u63d0\u4ea4<\/strong>\u00a0\u5230 DOM\uff08\u5c06\u83dc\u54c1\u653e\u5728\u684c\u5b50\u4e0a\uff09\u3011<\/mark><\/li>\n<\/ol>\n\n\n\n<p><em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>assemble <\/strong>\/\u0259\u02c8sembl\/ \u96c6\u5408\uff0c\u805a\u96c6\uff1b\u88c5\u914d\uff1b  <strong>ingredient <\/strong>\/\u026an\u02c8\u0261ri\u02d0di\u0259nt\/ (\u6df7\u5408\u7269\u7684)\u7ec4\u6210\u90e8\u5206; \u914d\u6599\uff1b<strong>scenario <\/strong>\/s\u0259\u02c8n\u00e6rio\u028a\/ \u60c5\u8282\uff1b\u5267\u672c\uff1b\u65b9\u6848<\/mark><\/em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\uff1b<\/mark><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-1-trigger-a-render\">Step 1: Trigger a render\u00a0\uff08\u89e6\u53d1\u4e00\u6b21\u6e32\u67d3\uff09<\/h2>\n\n\n\n<p>There are two reasons for a component to render\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u6709\u4e24\u79cd\u539f\u56e0\u4f1a\u5bfc\u81f4\u7ec4\u4ef6\u7684\u6e32\u67d3<\/mark><\/strong>\uff09:<\/p>\n\n\n\n<ol>\n<li>It\u2019s the component\u2019s\u00a0<strong>initial render.<\/strong> \uff08\u521d\u6b21\u6e32\u67d3\uff09<\/li>\n\n\n\n<li>The component\u2019s (or one of its ancestors\u2019)\u00a0<strong>state has been updated.<\/strong>\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u7ec4\u4ef6\u6216\u5176\u7956\u5148\u7684state\u53d1\u751f\u4e86\u6539\u53d8<\/strong><\/mark>\uff09<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"initial-render\">Initial render&nbsp;<a href=\"https:\/\/react.dev\/learn\/render-and-commit#initial-render\"><\/a><\/h3>\n\n\n\n<p>When your app starts, you need to trigger the initial render. Frameworks and sandboxes sometimes hide this code, but it\u2019s done by calling <a href=\"https:\/\/react.dev\/reference\/react-dom\/client\/createRoot\"><code>createRoot<\/code><\/a> with the target DOM node, and then calling its <code>render<\/code> method with your component\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u7136\u540e\u7528\u4f60\u7684\u7ec4\u4ef6\u8c03\u7528 <code>render<\/code> \u51fd\u6570\u5b8c\u6210\u7684<\/mark>\uff09:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default function Image() {\n  return (\n    &lt;img\n      src=\"https:\/\/i.imgur.com\/ZF6s192.jpg\"\n      alt=\"'Floralis Gen\u00e9rica' by Eduardo Catalano: a gigantic metallic flower sculpture with reflective petals\"\n    \/>\n  );\n}\n\nimport Image from '.\/Image.js';\nimport { createRoot } from 'react-dom\/client';\n\nconst root = createRoot(document.getElementById('root'))\n\/\/ render \u51fd\u6570\uff0c\u5982\u679c\u4f60\u6ce8\u91ca\u6389 root.render()\uff0c\u7136\u540e\u4f60\u5c06\u4f1a\u770b\u5230\u7ec4\u4ef6\u6d88\u5931\u3002\nroot.render(&lt;Image \/>);<\/code><\/pre>\n\n\n\n<p>Try commenting out the <code>root.render()<\/code> call and see the component disappear!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"re-renders-when-state-updates\">Re-renders when state updates\u00a0<a href=\"https:\/\/react.dev\/learn\/render-and-commit#re-renders-when-state-updates\"><\/a>\uff08\u72b6\u6001\u66f4\u65b0\u65f6\u91cd\u65b0\u6e32\u67d3\uff09<\/h3>\n\n\n\n<p>Once the component has been initially rendered,<strong> <\/strong>you can trigger further renders by updating its state with the <a href=\"https:\/\/react.dev\/reference\/react\/useState#setstate\"><code>set<\/code> function.<\/a> <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\uff08\u4e00\u65e6\u7ec4\u4ef6\u88ab\u521d\u6b21\u6e32\u67d3\uff0c\u4f60\u5c31\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 <a href=\"https:\/\/zh-hans.react.dev\/reference\/react\/useState#setstate\"><code>set<\/code> \u51fd\u6570<\/a> \u66f4\u65b0\u5176\u72b6\u6001\u6765\u89e6\u53d1\u4e4b\u540e\u7684\u6e32\u67d3\u3002\uff09<\/mark><\/strong><br>Updating your component\u2019s state automatically queues a render\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\">\u66f4\u65b0\u7ec4\u4ef6\u7684\u72b6\u6001\u4f1a\u81ea\u52a8\u5c06\u4e00\u6b21\u6e32\u67d3\u9001\u5165\u961f\u5217\u3002==\u300b\u9001\u5165\u961f\u5217\uff0c\u5e76\u4e0d\u4ee3\u8868\u7acb\u9a6c\u6e32\u67d3\uff01\uff01<\/mark><\/strong>\uff09. (You can imagine these as a restaurant guest ordering tea, dessert, and all sorts of things after putting in their first order, depending on the state of their thirst or hunger.)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-2-react-renders-your-components\">Step 2: React renders your components\u00a0<a href=\"https:\/\/react.dev\/learn\/render-and-commit#step-2-react-renders-your-components\"><\/a>\uff08React \u6e32\u67d3\u4f60\u7684\u7ec4\u4ef6\uff09<\/h2>\n\n\n\n<p>After you trigger a render, React calls your components to figure out what to display on screen. \uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5728\u4f60\u89e6\u53d1\u6e32\u67d3\u540e\uff0cReact \u4f1a\u8c03\u7528\u4f60\u7684\u7ec4\u4ef6\u6765\u786e\u5b9a\u8981\u5728\u5c4f\u5e55\u4e0a\u663e\u793a\u7684\u5185\u5bb9\u3002<\/mark>\uff09<strong>\u201cRendering\u201d is React calling your components.<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>On initial render,<\/strong>\u00a0React will call the root component.\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u5728\u8fdb\u884c\u521d\u6b21\u6e32\u67d3\u65f6,<\/strong>\u00a0React \u4f1a\u8c03\u7528\u6839\u7ec4\u4ef6\u3002<\/mark>\uff09<\/li>\n\n\n\n<li><strong>For subsequent renders,<\/strong>\u00a0React will call the function component whose state update triggered the render.\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u5bf9\u4e8e\u540e\u7eed\u7684\u6e32\u67d3,<\/strong>\u00a0React \u4f1a\u8c03\u7528\u5185\u90e8\u72b6\u6001\u66f4\u65b0\u89e6\u53d1\u4e86\u6e32\u67d3\u7684\u51fd\u6570\u7ec4\u4ef6\u3002<\/mark>\uff09<\/li>\n<\/ul>\n\n\n\n<p>This process is recursive: if the updated component returns some other component, React will render <em>that<\/em> component next, and if that component also returns something, it will render <em>that<\/em> component next, and so on. The process will continue until there are no more nested components and React knows exactly what should be displayed on screen.\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u9012\u5f52\u7684\uff1a\u5982\u679c\u66f4\u65b0\u540e\u7684\u7ec4\u4ef6\u4f1a\u8fd4\u56de\u67d0\u4e2a\u53e6\u5916\u7684\u7ec4\u4ef6\uff0c\u90a3\u4e48 React \u63a5\u4e0b\u6765\u5c31\u4f1a\u6e32\u67d3 <em>\u90a3\u4e2a<\/em> \u7ec4\u4ef6\uff0c\u8fd8\u662f\u770b\u4e0b\u9762\u7684\u4f8b\u5b50\u7406\u89e3\u5427<\/strong><\/mark>\uff09<\/p>\n\n\n\n<p>In the following example, React will call <code>Gallery()<\/code> and <code>Image()<\/code> several times:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Gallery.js\nexport default function Gallery() {\n  return (\n    &lt;section>\n      &lt;h1>\u9f13\u821e\u4eba\u5fc3\u7684\u96d5\u5851&lt;\/h1>\n      &lt;Image \/>\n      &lt;Image \/>\n      &lt;Image \/>\n    &lt;\/section>\n  );\n}\n\nfunction Image() {\n  return (\n    &lt;img\n      src=\"https:\/\/i.imgur.com\/ZF6s192.jpg\"\n      alt=\"'Floralis Gen\u00e9rica' by Eduardo Catalano: a gigantic metallic flower sculpture with reflective petals\"\n    \/>\n  );\n}\n\n\/\/ index.js\nimport Gallery from '.\/Gallery.js';\nimport { createRoot } from 'react-dom\/client';\n\nconst root = createRoot(document.getElementById('root'))\nroot.render(&lt;Gallery \/>);<\/code><\/pre>\n\n\n\n<ul>\n<li><strong>During the initial render,<\/strong>\u00a0React will\u00a0<a href=\"https:\/\/developer.mozilla.org\/docs\/Web\/API\/Document\/createElement\" target=\"_blank\" rel=\"noreferrer noopener\">create the DOM nodes<\/a>\u00a0for\u00a0<code>&lt;section><\/code>,\u00a0<code>&lt;h1><\/code>, and three\u00a0<code>&lt;img><\/code>\u00a0tags.\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\"><strong>\u521d\u6b21\u6e32\u67d3\u65f6\uff0cReact \u4f1a\u4e3a <code>&lt;section><\/code>,\u00a0<code>&lt;h1><\/code>, and three\u00a0<code>&lt;img><\/code>\u00a0tags \u521b\u5efa DOM \u8282\u70b9<\/strong><\/mark>\uff09<\/li>\n\n\n\n<li><strong>During a re-render,<\/strong>\u00a0React will calculate which of their properties, if any, have changed since the previous render. It won\u2019t do anything with that information until the next step, the commit phase.\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\"><strong>\u5728\u91cd\u65b0\u6e32\u67d3\u65f6\uff0cReact \u4f1a\u8ba1\u7b97\u81ea\u4e0a\u6b21\u6e32\u67d3\u4e4b\u540e\uff0c\u54ea\u4e9b\u5c5e\u6027\u6709\u53d8\u5316\uff0c\u5728\u4e0b\u4e00\u6b65\u63d0\u4ea4\u524d\uff0c\u4e0d\u4f1a\u5bf9\u8fd9\u4e9b\u4fe1\u606f\u8fdb\u884c\u4efb\u4f55\u64cd\u4f5c<\/strong><\/mark>\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Pitfall<\/h3>\n\n\n\n<p>Rendering must always be a <a href=\"https:\/\/react.dev\/learn\/keeping-components-pure\">pure calculation<\/a>:<\/p>\n\n\n\n<ul>\n<li><strong>Same inputs, same output.<\/strong>\u00a0Given the same inputs, a component should always return the same JSX. (When someone orders a salad with tomatoes, they should not receive a salad with onions!)<\/li>\n\n\n\n<li><strong>It minds its own business.<\/strong>\u00a0It should not change any objects or variables that existed before rendering. (One order should not change anyone else\u2019s order.)<\/li>\n<\/ul>\n\n\n\n<p>Otherwise\uff08\u5426\u5219\uff09, you can encounter\uff08\u9047\u5230\uff09 confusing bugs and <strong><em>unpredictable <\/em><\/strong>behavior as your codebase grows in complexity. When developing in \u201cStrict Mode\u201d, React calls each component\u2019s function twice, which can help surface mistakes caused by impure functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Optimizing performance\uff08\u6027\u80fd\u4f18\u5316\uff09<\/h3>\n\n\n\n<p>The default behavior of rendering all components nested within the updated component is not optimal for performance if the updated component is very high in the tree. If you run into a performance issue, there are several opt-in ways to solve it described in the <a href=\"https:\/\/reactjs.org\/docs\/optimizing-performance.html\" target=\"_blank\" rel=\"noreferrer noopener\">Performance<\/a> section. <strong>Don\u2019t optimize prematurely\uff08\u4e0d\u8981\u8fc7\u65e9\u4f18\u5316\uff09!<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"step-3-react-commits-changes-to-the-dom\">Step 3: React commits changes to the DOM\u00a0<a href=\"https:\/\/react.dev\/learn\/render-and-commit#step-3-react-commits-changes-to-the-dom\"><\/a>\uff08React \u628a\u66f4\u6539\u63d0\u4ea4\u5230 DOM \u4e0a\uff09<\/h2>\n\n\n\n<p>After rendering (calling) your components, React will modify the DOM\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5728\u6e32\u67d3\uff08\u8c03\u7528\uff09\u4f60\u7684\u7ec4\u4ef6\u4e4b\u540e\uff0cReact \u5c06\u4f1a\u4fee\u6539 DOM<\/mark>\uff09.<\/p>\n\n\n\n<ul>\n<li><strong>For the initial render,<\/strong>\u00a0React will use the\u00a0<a href=\"https:\/\/developer.mozilla.org\/docs\/Web\/API\/Node\/appendChild\" target=\"_blank\" rel=\"noreferrer noopener\"><code>appendChild()<\/code><\/a>\u00a0DOM API to put all the DOM nodes it has created on screen.<\/li>\n\n\n\n<li><strong>For re-renders,<\/strong>\u00a0React will apply the minimal necessary operations (calculated while rendering!) to make the DOM match the latest rendering output.\uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>\u5bf9\u4e8e<strong> re-renders<\/strong>\uff0c<\/strong>\u00a0React \u5c06\u5e94\u7528\u6700\u5c11\u7684\u5fc5\u8981\u64cd\u4f5c\uff08\u5728\u6e32\u67d3\u65f6\u8ba1\u7b97\uff01\uff09\uff0c\u4ee5\u4f7f\u5f97 DOM \u4e0e\u6700\u65b0\u7684\u6e32\u67d3\u8f93\u51fa\u76f8\u4e92\u5339\u914d\u3002<\/mark>\uff09<\/li>\n<\/ul>\n\n\n\n<p><strong>React only changes <em><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">the DOM nodes<\/mark><\/em> if there\u2019s a difference between renders.\uff08<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">React \u4ec5\u5728\u6e32\u67d3\u4e4b\u95f4\u5b58\u5728\u5dee\u5f02\u65f6\u624d\u4f1a\u66f4\u6539 DOM \u8282\u70b9<\/mark><\/strong>\uff09<\/strong> <br>For example, here is a component that re-renders with different props <strong><em>passed from<\/em><\/strong> its parent <strong><em>every second\uff08\u6bcf\u79d2\uff09<\/em><\/strong>. Notice how you can add some text into the <code>&lt;input><\/code>, updating its <code>value<\/code>, but the text doesn\u2019t disappear when the component re-renders\u3010<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-amber-color\">\u6211\u7406\u89e3\u662f\u56e0\u4e3a\u5f80\u91cc\u9762\u8f93\u5165\u503c\u5e76\u6ca1\u6709\u89e6\u53d1\u91cd\u65b0\u6e32\u67d3~~<\/mark><\/strong>\u3011:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export default function Clock({ time }) {\n  return (\n    &lt;>\n      &lt;h1>{time}&lt;\/h1>\n      &lt;input \/>\n    &lt;\/>\n  );\n}<\/code><\/pre>\n\n\n\n<p>This works because during this last step, React only updates the content of <code>&lt;h1&gt;<\/code> with the new <code>time<\/code>. It sees that the <code>&lt;input&gt;<\/code> appears in the JSX in the same place as last time, so React doesn\u2019t touch the <code>&lt;input&gt;<\/code>\u2014or its <code>value<\/code>!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"epilogue-browser-paint\">Epilogue: Browser paint\u00a0<a href=\"https:\/\/react.dev\/learn\/render-and-commit#epilogue-browser-paint\"><\/a>\uff08\u5c3e\u58f0\uff1a\u6d4f\u89c8\u5668\u7ed8\u5236\u00a0\uff09<\/h2>\n\n\n\n<p>After rendering is done and React updated the DOM, the browser will repaint the screen. \uff08<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u5728\u6e32\u67d3\u5b8c\u6210\u5e76\u4e14 React \u66f4\u65b0 DOM \u4e4b\u540e\uff0c\u6d4f\u89c8\u5668\u5c31\u4f1a\u91cd\u65b0\u7ed8\u5236\u5c4f\u5e55\u3002<\/mark>\uff09<br>Although this process is known as \u201cbrowser rendering\u201d, we\u2019ll refer to it as \u201cpainting\u201d to avoid confusion throughout the docs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"recap\">Recap<a href=\"https:\/\/react.dev\/learn\/render-and-commit#recap\"><\/a><\/h2>\n\n\n\n<ul>\n<li>Any screen update in a React app happens in three steps:\n<ol>\n<li>Trigger<\/li>\n\n\n\n<li>Render<\/li>\n\n\n\n<li>Commit<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>You can use Strict Mode to find mistakes in your components<\/li>\n\n\n\n<li>React does not touch the DOM if the rendering result is the same as last time<\/li>\n<\/ul>\n\n\n\n<ul>\n<li>\u5728\u4e00\u4e2a React \u5e94\u7528\u4e2d\uff0c\u4e00\u6b21\u5c4f\u5e55\u66f4\u65b0\u90fd\u4f1a\u53d1\u751f\u4ee5\u4e0b\u4e09\u4e2a\u6b65\u9aa4\uff1a\n<ol>\n<li>\u89e6\u53d1<\/li>\n\n\n\n<li>\u6e32\u67d3<\/li>\n\n\n\n<li>\u63d0\u4ea4<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e25\u683c\u6a21\u5f0f\u53bb\u627e\u5230\u7ec4\u4ef6\u4e2d\u7684\u9519\u8bef<\/li>\n\n\n\n<li>\u5982\u679c\u6e32\u67d3\u7ed3\u679c\u4e0e\u4e0a\u6b21\u4e00\u6837\uff0c\u90a3\u4e48 React \u5c06\u4e0d\u4f1a\u4fee\u6539 DOM<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>\u53c2\uff1a<a href=\"https:\/\/zh-hans.react.dev\/learn\/render-and-commit\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/zh-hans.react.dev\/learn\/render-and-commit<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6e32\u67d3\u548c\u63d0\u4ea4 Before your components are displayed on screen, t&#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>Render and Commit - \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\/render-and-commit\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Render and Commit - \u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"og:description\" content=\"\u6e32\u67d3\u548c\u63d0\u4ea4 Before your components are displayed on screen, t...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/\" \/>\n<meta property=\"og:site_name\" content=\"\u7801\u5148\u751f\u7684\u535a\u5ba2\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-28T10:57:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-28T10:58:00+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=\"6 \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\/render-and-commit\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/\"},\"author\":{\"name\":\"\u7801\u5148\u751f\",\"@id\":\"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf\"},\"headline\":\"Render and Commit\",\"datePublished\":\"2024-07-28T10:57:38+00:00\",\"dateModified\":\"2024-07-28T10:58:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/\"},\"wordCount\":881,\"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\/render-and-commit\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/\",\"url\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/\",\"name\":\"Render and Commit - \u7801\u5148\u751f\u7684\u535a\u5ba2\",\"isPartOf\":{\"@id\":\"http:\/\/codermr.com\/#website\"},\"datePublished\":\"2024-07-28T10:57:38+00:00\",\"dateModified\":\"2024-07-28T10:58:00+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"http:\/\/codermr.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Render and Commit\"}]},{\"@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":"Render and Commit - \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\/render-and-commit\/","og_locale":"zh_CN","og_type":"article","og_title":"Render and Commit - \u7801\u5148\u751f\u7684\u535a\u5ba2","og_description":"\u6e32\u67d3\u548c\u63d0\u4ea4 Before your components are displayed on screen, t...","og_url":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/","og_site_name":"\u7801\u5148\u751f\u7684\u535a\u5ba2","article_published_time":"2024-07-28T10:57:38+00:00","article_modified_time":"2024-07-28T10:58:00+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":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/#article","isPartOf":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/"},"author":{"name":"\u7801\u5148\u751f","@id":"http:\/\/codermr.com\/#\/schema\/person\/39016e15c79e4f02d1ed3a64688619bf"},"headline":"Render and Commit","datePublished":"2024-07-28T10:57:38+00:00","dateModified":"2024-07-28T10:58:00+00:00","mainEntityOfPage":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/"},"wordCount":881,"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\/render-and-commit\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/","url":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/","name":"Render and Commit - \u7801\u5148\u751f\u7684\u535a\u5ba2","isPartOf":{"@id":"http:\/\/codermr.com\/#website"},"datePublished":"2024-07-28T10:57:38+00:00","dateModified":"2024-07-28T10:58:00+00:00","breadcrumb":{"@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/codermr.com\/index.php\/2024\/07\/28\/render-and-commit\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/codermr.com\/"},{"@type":"ListItem","position":2,"name":"Render and Commit"}]},{"@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\/1254"}],"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=1254"}],"version-history":[{"count":4,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1254\/revisions"}],"predecessor-version":[{"id":1258,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/posts\/1254\/revisions\/1258"}],"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=1254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/categories?post=1254"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/codermr.com\/index.php\/wp-json\/wp\/v2\/tags?post=1254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}