{"id":5715,"date":"2022-03-20T19:16:28","date_gmt":"2022-03-20T11:16:28","guid":{"rendered":"http:\/\/39.106.39.94\/?p=5715"},"modified":"2022-09-07T14:24:40","modified_gmt":"2022-09-07T06:24:40","slug":"react%e7%bb%84%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/lilichao.com\/?p=5715","title":{"rendered":"React\u7ec4\u4ef6"},"content":{"rendered":"\n<p>\u5728React\u4e2d\u7f51\u9875\u88ab\u62c6\u5206\u4e3a\u4e86\u4e00\u4e2a\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u7ec4\u4ef6\u662f\u72ec\u7acb\u53ef\u590d\u7528\u7684\u4ee3\u7801\u7247\u6bb5\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u7ec4\u4ef6\u53ef\u80fd\u662f\u9875\u9762\u4e2d\u7684\u4e00\u4e2a\u6309\u94ae\uff0c\u4e00\u4e2a\u5bf9\u8bdd\u6846\uff0c\u4e00\u4e2a\u5f39\u51fa\u5c42\u7b49\u3002React\u4e2d\u5b9a\u4e49\u7ec4\u4ef6\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff1a\u57fa\u4e8e\u51fd\u6570\u7684\u7ec4\u4ef6\u548c\u57fa\u4e8e\u7c7b\u7684\u7ec4\u4ef6\u3002\u672c\u8282\u6211\u4eec\u5148\u770b\u770b\u57fa\u4e8e\u51fd\u6570\u7684\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<p>\u57fa\u4e8e\u51fd\u6570\u7684\u7ec4\u4ef6\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u4f1a\u8fd4\u56deJSX\uff08React\u5143\u7d20\uff09\u7684\u666e\u901a\u7684JS\u51fd\u6570\uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import ReactDOM from \"react-dom\/client\";\n\u200b\n\/\/ \u8fd9\u5c31\u662f\u4e00\u4e2a\u7ec4\u4ef6\nfunction App(){\n &nbsp; &nbsp;return &lt;h1&gt;\u6211\u662f\u4e00\u4e2aReact\u7684\u7ec4\u4ef6\uff01&lt;\/h1&gt;\n}\n\u200b\nconst root = ReactDOM.createRoot(document.getElementById('root'));\nroot.render(&lt;App\/&gt;);<\/pre>\n\n\n\n<p>\u51fd\u6570\u5f0f\u7ec4\u4ef6\u4e3b\u8981\u6709\u4e24\u4e2a\u6ce8\u610f\u70b9\uff1a<\/p>\n\n\n\n<ol><li>\u51fd\u6570\u540d\u9996\u5b57\u6bcd\u5927\u5199<\/li><li>\u8fd4\u56de\u503c\u662f\u4e00\u4e2aJSX\uff08React\u5143\u7d20\uff09<\/li><\/ol>\n\n\n\n<p>\u4e3a\u4e86\u4f7f\u5f97\u9879\u76ee\u7ed3\u6784\u66f4\u52a0\u7684\u6e05\u6670\uff0c\u66f4\u6613\u4e8e\u7ef4\u62a4\uff0c\u6bcf\u4e2a\u7ec4\u4ef6\u901a\u5e38\u4f1a\u5b58\u50a8\u5230\u4e00\u4e2a\u5355\u72ec\u7684\u6587\u4ef6\u4e2d\uff0c\u6bd4\u5982\u4e0a\u4f8b\u4e2d\u7684App\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u5b58\u50a8\u5230App.js\u4e2d\uff0c\u5e76\u901a\u8fc7export\u5bfc\u51fa\u3002<\/p>\n\n\n\n<p><code>App.js<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function App(){\n &nbsp; &nbsp;return &lt;h1&gt;\u6211\u662f\u4e00\u4e2aReact\u7684\u7ec4\u4ef6\uff01&lt;\/h1&gt;\n}\n\u200b\nexport default App;<\/pre>\n\n\n\n<p>\u6216\u8005\u4f7f\u7528\u7bad\u5934\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const App = () =&gt; {\n &nbsp; &nbsp;return &lt;h1&gt;\u6211\u662f\u4e00\u4e2aReact\u7684\u7ec4\u4ef6\uff01&lt;\/h1&gt;;\n};\n\u200b\nexport default App;<\/pre>\n\n\n\n<p>\u5728\u5176\u4ed6\u6587\u4ef6\u4e2d\u4f7f\u7528\u65f6\uff0c\u9700\u8981\u5148\u901a\u8fc7import\u8fdb\u884c\u5f15\u5165\uff1a<\/p>\n\n\n\n<p><code>index.js<\/code><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import ReactDOM from \"react-dom\/client\";\nimport App from \".\/App\";\n\u200b\nconst root = ReactDOM.createRoot(document.getElementById('root'));\nroot.render(&lt;App\/&gt;);<\/pre>\n\n\n\n<p>\u5f15\u5165\u540e\u901a\u8fc7<code>&lt;\u7ec4\u4ef6\u540d\/&gt;<\/code>\u6216<code>&lt;\u7ec4\u4ef6\u540d&gt;&lt;\/\u7ec4\u4ef6\u540d&gt;<\/code>\u5373\u53ef\u5f15\u5165\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<p>\u5728\u4e00\u4e2a\u7ec4\u4ef6\u4e2d\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5176\u4ed6\u7ec4\u4ef6\uff0c\u6bd4\u5982\u6709\u5982\u4e0b<code>Button.js<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const Button = () =&gt; {\n &nbsp; &nbsp;return &lt;button&gt;\u6211\u662f\u4e00\u4e2a\u6309\u94ae&lt;\/button&gt;;\n};\n\u200b\nexport default Button;<\/pre>\n\n\n\n<p>\u5728<code>App.js<\/code>\u4e2d\u53ef\u4ee5\u76f4\u63a5\u5f15\u5165\u8be5\u7ec4\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import Button from \".\/Button\";\n\u200b\nconst App = () =&gt; {\n &nbsp; &nbsp;return &lt;div&gt;\n &nbsp; &nbsp; &nbsp; &nbsp;&lt;h1&gt;\u6211\u662f\u4e00\u4e2aReact\u7684\u7ec4\u4ef6\uff01&lt;\/h1&gt;\n &nbsp; &nbsp; &nbsp; &nbsp;&lt;Button\/&gt;\n &nbsp; &nbsp;&lt;\/div&gt;;\n};\n\u200b\nexport default App;<\/pre>\n\n\n\n<p>\u9664\u4e86\u51fd\u6570\u7ec4\u4ef6\u5916\uff0c\u5728React\u4e2d\u8fd8\u6709\u4e00\u79cd\u7c7b\u7ec4\u4ef6\uff0c\u4f46\u7c7b\u7ec4\u4ef6\u4f7f\u7528\u8d77\u6765\u5e76\u4e0d\u65b9\u4fbf\uff0c\u6240\u4ee5\u5728React\u4e2d\u7c7b\u7ec4\u4ef6\u7684\u4f7f\u7528\u573a\u666f\u8d8a\u6765\u8d8a\u5c11\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u5148\u5c06\u7c7b\u7ec4\u4ef6\u653e\u4e0b\u6682\u4e14\u4e0d\u8868\uff0c\u540e\u7eed\u8bfe\u7a0b\u4e2d\u4f1a\u5bf9\u6709\u5bf9\u7c7b\u7ec4\u4ef6\u7684\u8865\u5145\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5f15\u5165\u6837\u5f0f<\/h2>\n\n\n\n<p>\u90a3\u4e48\u5982\u4f55\u4e3aReact\u7ec4\u4ef6\u5f15\u5165\u6837\u5f0f\u5462\uff1f\u5f88\u7b80\u5355\u76f4\u63a5\u5728\u7ec4\u4ef6\u4e2dimport\u5373\u53ef\u3002\u4f8b\u5982\uff1a\u6211\u4eec\u6253\u7b97\u4e3aButton\u7ec4\u4ef6\u7f16\u5199\u4e00\u7ec4\u6837\u5f0f\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5230Button.css\u4e2d\u3002\u6211\u4eec\u53ea\u9700\u8981\u76f4\u63a5\u5728Button.js\u4e2d\u5f15\u5165Button.css\u5c31\u80fd\u8f7b\u6613\u5b8c\u6210\u6837\u5f0f\u7684\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<p>Button.css\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>button{\n    background-color: #bfa;\n}\n<\/code><\/pre>\n\n\n\n<p>Button.js\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import '.\/Button.css';\nconst Button = () =&gt; {\n    return &lt;button&gt;\u6211\u662f\u4e00\u4e2a\u6309\u94ae&lt;\/button&gt;;\n};\nexport default Button;<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\u5f15\u5165\u7684\u6837\u5f0f\uff0c\u9700\u8981\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n\n\n\n<ol><li>CSS\u5c31\u662f\u6807\u51c6\u7684CSS\u8bed\u6cd5\uff0c\u5404\u79cd\u9009\u62e9\u5668\u3001\u6837\u5f0f\u3001\u5a92\u4f53\u67e5\u8be2\u4e4b\u7c7b\u6b63\u5e38\u5199\u5373\u53ef\u3002<\/li><li>\u5c3d\u91cf\u5c06js\u6587\u4ef6\u548ccss\u6587\u4ef6\u7684\u6587\u4ef6\u540d\u8bbe\u7f6e\u4e3a\u76f8\u540c\u7684\u6587\u4ef6\u540d\u3002<\/li><li>\u5f15\u5165\u6837\u5f0f\u65f6\u76f4\u63a5import\uff0c\u65e0\u9700\u6307\u5b9a\u540d\u5b57\uff0c\u4e14\u5f15\u5165\u6837\u5f0f\u5fc5\u987b\u4ee5.\/\u6216..\/\u5f00\u5934\u3002<\/li><li>\u8fd9\u79cd\u5f62\u5f0f\u5f15\u5165\u7684\u6837\u5f0f\u662f\u5168\u5c40\u6837\u5f0f\uff0c\u6709\u53ef\u80fd\u4f1a\u88ab\u5176\u4ed6\u6837\u5f0f\u8986\u76d6\u3002<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\u5728React\u4e2d\u7f51\u9875\u88ab\u62c6\u5206\u4e3a\u4e86\u4e00\u4e2a\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u7ec4\u4ef6\u662f\u72ec\u7acb\u53ef\u590d\u7528\u7684\u4ee3\u7801\u7247\u6bb5\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u7ec4\u4ef6\u53ef\u80fd\u662f\u9875\u9762\u4e2d\u7684\u4e00\u4e2a\u6309\u94ae\uff0c\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5717,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[198],"tags":[200,202,218,199],"_links":{"self":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5715"}],"collection":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5715"}],"version-history":[{"count":4,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5715\/revisions"}],"predecessor-version":[{"id":6406,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5715\/revisions\/6406"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/media\/5717"}],"wp:attachment":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}