{"id":5632,"date":"2022-03-23T08:15:39","date_gmt":"2022-03-23T00:15:39","guid":{"rendered":"http:\/\/39.106.39.94\/?p=5632"},"modified":"2022-05-08T16:17:41","modified_gmt":"2022-05-08T08:17:41","slug":"fragment","status":"publish","type":"post","link":"https:\/\/lilichao.com\/?p=5632","title":{"rendered":"Fragment"},"content":{"rendered":"\n<p>\u5728React\u4e2d\uff0cJSX\u5fc5\u987b\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u6839\u5143\u7d20\u3002\u8fd9\u5c31\u5bfc\u81f4\u4e86\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\u6211\u4eec\u4e0d\u5f97\u4e0d\u5728\u5b50\u5143\u7d20\u7684\u5916\u90e8\u6dfb\u52a0\u4e00\u4e2a\u989d\u5916\u7684\u7236\u5143\u7d20\uff0c\u50cf\u662f\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react';\r\n\r\nconst MyComponent = () => {\r\n    return (\r\n        &lt;div>\r\n            &lt;div>\u6211\u662f\u7ec4\u4ef61&lt;\/div>\r\n            &lt;div>\u6211\u662f\u7ec4\u4ef62&lt;\/div>\r\n            &lt;div>\u6211\u662f\u7ec4\u4ef63&lt;\/div>\r\n        &lt;\/div>\r\n    );\r\n};\r\n\r\nexport default MyComponent;<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8fb9\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u7ec4\u4ef6\u5185\u90e8\u9700\u8981\u5f15\u5165\u4e09\u4e2a\u7ec4\u4ef6\uff08\u4f7f\u7528\u4e09\u4e2adiv\u8868\u793a\uff09\u3002\u7531\u4e8e\u662f\u4e09\u4e2a\u7ec4\u4ef6\uff0c\u6839\u636eJSX\u7684\u8bed\u6cd5\u5fc5\u987b\u5728\u4e09\u4e2a\u7ec4\u4ef6\u7684\u5916\u90e8\u5728\u5957\u4e00\u4e2adiv\uff0c\u4e09\u4e2a\u7ec4\u4ef6\u624d\u80fd\u591f\u6b63\u5e38\u4f7f\u7528\uff0c\u4f46\u662f\u8fd9\u4e2a\u5916\u5c42\u7684div\u5728\u6700\u7ec8\u7684\u9875\u9762\u4e2d\u6ca1\u6709\u4efb\u4f55\u7684\u5b9e\u8d28\u6027\u4f5c\u7528\u3002<\/p>\n\n\n\n<p>\u9047\u5230\u8fd9\u79cd\u60c5\u51b5\u6211\u4eec\u5c31\u975e\u5e38\u5e0c\u671b\u80fd\u6709\u4e00\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5f15\u5165\u591a\u4e2a\u7ec4\u4ef6\uff0c\u4f46\u53c8\u4e0d\u4f1a\u5728\u6700\u7ec8\u7684\u7f51\u9875\u4e2d\u6dfb\u52a0\u591a\u4f59\u7684\u7ed3\u6784\uff0c\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u8fd9\u6837\u4e00\u4e2a\u7ec4\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react';\r\n\r\nconst MyFragment = (props) => {\r\n    return props.children;\r\n};\r\n\r\nexport default MyFragment;<\/code><\/pre>\n\n\n\n<p>MyFragment\u8fd9\u4e2a\u7ec4\u4ef6\u975e\u5e38\u7b80\u5355\uff0c\u5b83\u4f1a\u76f4\u63a5\u8fd4\u56de\u5f53\u524d\u7ec4\u4ef6\u5185\u90e8\u7684\u6240\u6709\u5b50\u5143\u7d20\uff0c\u4e0d\u4f1a\u4ea7\u751f\u65b0\u7684\u5143\u7d20\uff0c\u53ef\u4ee5\u5c06\u4e0a\u8fb9\u7ec4\u4ef6\u4e2d\u7684\u5916\u5c42div\uff0c\u4fee\u6539\u4e3aMyFragmet\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import React from 'react';\nimport MyFragment from \".\/MyFragment\";\n\nconst MyComponent = () => {\n    return (\n        &lt;MyFragment>\n            &lt;div>\u6211\u662f\u7ec4\u4ef61&lt;\/div>\n            &lt;div>\u6211\u662f\u7ec4\u4ef62&lt;\/div>\n            &lt;div>\u6211\u662f\u7ec4\u4ef63&lt;\/div>\n        &lt;\/MyFragment>\n    );\n};\n\nexport default MyComponent;<\/pre>\n\n\n\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u7f51\u9875\u4e2d\u5c31\u4e0d\u4f1a\u51fa\u73b0\u591a\u4f59\u7684div\u4e86\u3002<\/p>\n\n\n\n<p>\u5b9e\u9645\u4e0a\u5728React\u4e2d\u5df2\u7ecf\u4e3a\u6211\u4eec\u63d0\u4f9b\u597d\u4e86\u4e00\u4e2a\u73b0\u6210\u7684\u7ec4\u4ef6\u5e2e\u52a9\u6211\u4eec\u5b8c\u6210\u8fd9\u4e2a\u5de5\u4f5c\uff0c\u8fd9\u4e2a\u7ec4\u4ef6\u53ef\u4ee5\u901a\u8fc7React.Fragment\u4f7f\u7528\uff0c\u4e0a\u8ff0\u6848\u4f8b\uff0c\u4e5f\u53ef\u4ee5\u4fee\u6539\u6210\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import <strong><em>React <\/em><\/strong>from 'react';<br><br>const MyComponent = () =&gt; {<br>    return (<br>        &lt;React.Fragment&gt;<br>            &lt;div&gt;\u6211\u662f\u7ec4\u4ef61&lt;\/div&gt;<br>            &lt;div&gt;\u6211\u662f\u7ec4\u4ef62&lt;\/div&gt;<br>            &lt;div&gt;\u6211\u662f\u7ec4\u4ef63&lt;\/div&gt;<br>        &lt;\/React.Fragment&gt;<br>    );<br>};<br><br>export default MyComponent;<\/pre>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u4fee\u6539\u5f15\u5165React\u7684\u65b9\u5f0f\uff0c\u76f4\u63a5\u4f7f\u7528Fragment\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import <strong><em>React<\/em><\/strong>, {<strong><em>Fragment<\/em><\/strong>} from 'react';<br><br>const MyComponent = () =&gt; {<br>    return (<br>        &lt;Fragment&gt;<br>            &lt;div&gt;\u6211\u662f\u7ec4\u4ef61&lt;\/div&gt;<br>            &lt;div&gt;\u6211\u662f\u7ec4\u4ef62&lt;\/div&gt;<br>            &lt;div&gt;\u6211\u662f\u7ec4\u4ef63&lt;\/div&gt;<br>        &lt;\/Fragment&gt;<br>    );<br>};<br><br>export default MyComponent;<\/pre>\n\n\n\n<p>\u4e0d\u8fc7\u6700\u723d\u7684\u662f\uff0c\u5728React\u4e2d\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u4fbf\u6377\u7684\u65b9\u5f0f\uff0c\u76f4\u63a5\u4f7f\u7528&lt;>&lt;\/>\u4ee3\u66ffFragment\u66f4\u52a0\u7b80\u5355\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from 'react';\r\n\r\nconst MyComponent = () => {\r\n    return (\r\n        &lt;>\r\n            &lt;div>\u6211\u662f\u7ec4\u4ef61&lt;\/div>\r\n            &lt;div>\u6211\u662f\u7ec4\u4ef62&lt;\/div>\r\n            &lt;div>\u6211\u662f\u7ec4\u4ef63&lt;\/div>\r\n        &lt;\/>\r\n    );\r\n};\r\n\r\nexport default MyComponent;\r\n<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728React\u4e2d\uff0cJSX\u5fc5\u987b\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u6839\u5143\u7d20\u3002\u8fd9\u5c31\u5bfc\u81f4\u4e86\u5728\u6709\u4e9b\u60c5\u51b5\u4e0b\u6211\u4eec\u4e0d\u5f97\u4e0d\u5728\u5b50\u5143\u7d20\u7684\u5916\u90e8\u6dfb\u52a0\u4e00\u4e2a\u989d\u5916\u7684\u7236\u5143 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5637,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[198],"tags":[200,202,199,201,203],"_links":{"self":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5632"}],"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=5632"}],"version-history":[{"count":4,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5632\/revisions"}],"predecessor-version":[{"id":5636,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5632\/revisions\/5636"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/media\/5637"}],"wp:attachment":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}