{"id":207518,"date":"2020-12-22T09:13:07","date_gmt":"2020-12-22T01:13:07","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=207518"},"modified":"2020-12-21T10:14:25","modified_gmt":"2020-12-21T02:14:25","slug":"react-study-jsx","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/react-study-jsx.html","title":{"rendered":"React\u5b66\u4e60\u7b14\u8bb0-JSX"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nReact \u662f\u4e00\u4e2a\u7528\u4e8e\u6784\u5efa\u7528\u6237\u754c\u9762\u7684 JAVASCRIPT \u5e93\u3002React \u4e3b\u8981\u7528\u4e8e\u6784\u5efaUI\uff0c\u5f88\u591a\u4eba\u8ba4\u4e3a React \u662f MVC \u4e2d\u7684 V\uff08\u89c6\u56fe\uff09\u3002React \u8d77\u6e90\u4e8e Facebook \u7684\u5185\u90e8\u9879\u76ee\uff0c\u7528\u6765\u67b6\u8bbe Instagram \u7684\u7f51\u7ad9\uff0c\u5e76\u4e8e 2013 \u5e74 5 \u6708\u5f00\u6e90\u3002React \u62e5\u6709\u8f83\u9ad8\u7684\u6027\u80fd\uff0c\u4ee3\u7801\u903b\u8f91\u975e\u5e38\u7b80\u5355\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4eba\u5df2\u5f00\u59cb\u5173\u6ce8\u548c\u4f7f\u7528\u5b83\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\nReact \u4f7f\u7528 JSX \u6765\u66ff\u4ee3\u5e38\u89c4\u7684 JavaScript\u3002
\nJSX \u662f\u4e00\u4e2a\u770b\u8d77\u6765\u5f88\u50cf XML \u7684 JavaScript \u8bed\u6cd5\u6269\u5c55\u3002
\n\u6211\u4eec\u4e0d\u9700\u8981\u4e00\u5b9a\u4f7f\u7528 JSX\uff0c\u4f46\u5b83\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n

    \n
  • 1.JSX \u6267\u884c\u66f4\u5feb\uff0c\u56e0\u4e3a\u5b83\u5728\u7f16\u8bd1\u4e3a JavaScript \u4ee3\u7801\u540e\u8fdb\u884c\u4e86\u4f18\u5316\u3002<\/li>\n
  • 2.\u5b83\u662f\u7c7b\u578b\u5b89\u5168\u7684\uff0c\u5728\u7f16\u8bd1\u8fc7\u7a0b\u4e2d\u5c31\u80fd\u53d1\u73b0\u9519\u8bef\u3002<\/li>\n
  • 3.\u4f7f\u7528 JSX \u7f16\u5199\u6a21\u677f\u66f4\u52a0\u7b80\u5355\u5feb\u901f\u3002<\/li>\n<\/ul>\n

    \u6211\u4eec\u5148\u770b\u4e0b\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n

    \r\nconst element = <h1>Hello, world!<\/h1>;<\/pre>\n

    \u8fd9\u79cd\u770b\u8d77\u6765\u53ef\u80fd\u6709\u4e9b\u5947\u602a\u7684\u6807\u7b7e\u8bed\u6cd5\u65e2\u4e0d\u662f\u5b57\u7b26\u4e32\u4e5f\u4e0d\u662f HTML\u3002
    \n\u5b83\u88ab\u79f0\u4e3a JSX\uff0c \u4e00\u79cd JavaScript \u7684\u8bed\u6cd5\u6269\u5c55\u3002 \u6211\u4eec\u63a8\u8350\u5728 React \u4e2d\u4f7f\u7528 JSX \u6765\u63cf\u8ff0\u7528\u6237\u754c\u9762\u3002
    \nJSX \u662f\u5728 JavaScript \u5185\u90e8\u5b9e\u73b0\u7684\u3002
    \n\u6211\u4eec\u77e5\u9053\u5143\u7d20\u662f\u6784\u6210 React \u5e94\u7528\u7684\u6700\u5c0f\u5355\u4f4d\uff0cJSX \u5c31\u662f\u7528\u6765\u58f0\u660e React \u5f53\u4e2d\u7684\u5143\u7d20\u3002
    \n\u4e0e\u6d4f\u89c8\u5668\u7684 DOM \u5143\u7d20\u4e0d\u540c\uff0cReact \u5f53\u4e2d\u7684\u5143\u7d20\u4e8b\u5b9e\u4e0a\u662f\u666e\u901a\u7684\u5bf9\u8c61\uff0cReact DOM \u53ef\u4ee5\u786e\u4fdd \u6d4f\u89c8\u5668 DOM \u7684\u6570\u636e\u5185\u5bb9\u4e0e React \u5143\u7d20\u4fdd\u6301\u4e00\u81f4\u3002
    \n\u8981\u5c06 React \u5143\u7d20\u6e32\u67d3\u5230\u6839 DOM \u8282\u70b9\u4e2d\uff0c\u6211\u4eec\u901a\u8fc7\u628a\u5b83\u4eec\u90fd\u4f20\u9012\u7ed9 ReactDOM.render() \u7684\u65b9\u6cd5\u6765\u5c06\u5176\u6e32\u67d3\u5230\u9875\u9762\u4e0a\uff1a<\/p>\n

    React \u5b9e\u4f8b<\/p>\n

    \r\nvar myDivElement = <div className=\"foo\" \/>;\r\nReactDOM.render(myDivElement, document.getElementById('example'));<\/pre>\n

    \u6ce8\u610f:
    \n\u7531\u4e8e JSX \u5c31\u662f JavaScript\uff0c\u4e00\u4e9b\u6807\u8bc6\u7b26\u50cf class \u548c for \u4e0d\u5efa\u8bae\u4f5c\u4e3a XML \u5c5e\u6027\u540d\u3002\u4f5c\u4e3a\u66ff\u4ee3\uff0cReact DOM \u4f7f\u7528 className \u548c htmlFor \u6765\u505a\u5bf9\u5e94\u7684\u5c5e\u6027\u3002<\/p>\n

    \u4f7f\u7528 JSX<\/strong><\/div>\n

    JSX \u770b\u8d77\u6765\u7c7b\u4f3c HTML \uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u4e0b\u5b9e\u4f8b:<\/p>\n

    \r\nReactDOM.render(\r\n    <h1>Hello, world!<\/h1>,\r\n    document.getElementById('example')\r\n);<\/pre>\n

    \u6211\u4eec\u53ef\u4ee5\u5728\u4ee5\u4e0a\u4ee3\u7801\u4e2d\u5d4c\u5957\u591a\u4e2a HTML \u6807\u7b7e\uff0c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a div \u5143\u7d20\u5305\u88f9\u5b83\uff0c\u5b9e\u4f8b\u4e2d\u7684 p \u5143\u7d20\u6dfb\u52a0\u4e86\u81ea\u5b9a\u4e49\u5c5e\u6027 data-myattribute\uff0c\u6dfb\u52a0\u81ea\u5b9a\u4e49\u5c5e\u6027\u9700\u8981\u4f7f\u7528 data- \u524d\u7f00\u3002<\/p>\n

    React \u5b9e\u4f8b<\/p>\n

    \r\nReactDOM.render(\r\n    <div>\r\n    <h1>Linux\u5c31\u8be5\u8fd9\u4e48\u5b66<\/h1>\r\n    <h2>\u6b22\u8fce\u5b66\u4e60 React<\/h2>\r\n    <p data-myattribute = \"somevalue\">\u8fd9\u662f\u4e00\u4e2a\u5f88\u4e0d\u9519\u7684 JavaScript \u5e93!<\/p>\r\n    <\/div>\r\n    ,\r\n    document.getElementById('example')\r\n);\r\n<\/pre>\n
    \u72ec\u7acb\u6587\u4ef6<\/strong><\/div>\n

    \u4f60\u7684 React JSX \u4ee3\u7801\u53ef\u4ee5\u653e\u5728\u4e00\u4e2a\u72ec\u7acb\u6587\u4ef6\u4e0a\uff0c\u4f8b\u5982\u6211\u4eec\u521b\u5efa\u4e00\u4e2a helloworld_react.js \u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

    \r\nReactDOM.render(\r\n  <h1>Hello, world!<\/h1>,\r\n  document.getElementById('example')\r\n);<\/pre>\n

    \u7136\u540e\u5728 HTML \u6587\u4ef6\u4e2d\u5f15\u5165\u8be5 JS \u6587\u4ef6\uff1a<\/p>\n

    React \u5b9e\u4f8b<\/p>\n

    \r\n<body>\r\n  <div id=\"example\"><\/div>\r\n<script type=\"text\/babel\" src=\"helloworld_react.js\"><\/script>\r\n<\/body><\/pre>\n
    JavaScript \u8868\u8fbe\u5f0f<\/strong><\/div>\n

    \u6211\u4eec\u53ef\u4ee5\u5728 JSX \u4e2d\u4f7f\u7528 JavaScript \u8868\u8fbe\u5f0f\u3002\u8868\u8fbe\u5f0f\u5199\u5728\u82b1\u62ec\u53f7 {} \u4e2d\u3002\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n

    React \u5b9e\u4f8b<\/p>\n

    \r\nReactDOM.render(\r\n    <div>\r\n      <h1>{1+1}<\/h1>\r\n    <\/div>\r\n    ,\r\n    document.getElementById('example')\r\n);\r\n<\/pre>\n

    \u5728 JSX \u4e2d\u4e0d\u80fd\u4f7f\u7528 if else \u8bed\u53e5\uff0c\u4f46\u53ef\u4ee5\u4f7f\u7528 conditional (\u4e09\u5143\u8fd0\u7b97) \u8868\u8fbe\u5f0f\u6765\u66ff\u4ee3\u3002\u4ee5\u4e0b\u5b9e\u4f8b\u4e2d\u5982\u679c\u53d8\u91cf i \u7b49\u4e8e 1 \u6d4f\u89c8\u5668\u5c06\u8f93\u51fa true, \u5982\u679c\u4fee\u6539 i \u7684\u503c\uff0c\u5219\u4f1a\u8f93\u51fa false.<\/p>\n

    React \u5b9e\u4f8b<\/p>\n

    \r\nReactDOM.render(\r\n    <div>\r\n      <h1>{i == 1 ? 'True!' : 'False'}<\/h1>\r\n    <\/div>\r\n    ,\r\n    document.getElementById('example')\r\n);\r\n<\/pre>\n
    \u6837\u5f0f<\/strong><\/div>\n

    React \u63a8\u8350\u4f7f\u7528\u5185\u8054\u6837\u5f0f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 camelCase \u8bed\u6cd5\u6765\u8bbe\u7f6e\u5185\u8054\u6837\u5f0f. React \u4f1a\u5728\u6307\u5b9a\u5143\u7d20\u6570\u5b57\u540e\u81ea\u52a8\u6dfb\u52a0 px \u3002\u4ee5\u4e0b\u5b9e\u4f8b\u6f14\u793a\u4e86\u4e3a h1 \u5143\u7d20\u6dfb\u52a0 myStyle \u5185\u8054\u6837\u5f0f\uff1a<\/p>\n

    React \u5b9e\u4f8b<\/p>\n

    \r\nvar myStyle = {\r\n    fontSize: 100,\r\n    color: '#FF0000'\r\n};\r\nReactDOM.render(\r\n    <h1 style = {myStyle}>Linux\u5c31\u8be5\u8fd9\u4e48\u5b66<\/h1>,\r\n    document.getElementById('example')\r\n);<\/pre>\n
    \u6ce8\u91ca<\/strong><\/div>\n

    \u6ce8\u91ca\u9700\u8981\u5199\u5728\u82b1\u62ec\u53f7\u4e2d\uff0c\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n

    React \u5b9e\u4f8b<\/p>\n

    \r\nReactDOM.render(\r\n    <div>\r\n    <h1>Linux\u5c31\u8be5\u8fd9\u4e48\u5b66<\/h1>\r\n    {\/*\u6ce8\u91ca...*\/}\r\n     <\/div>,\r\n    document.getElementById('example')\r\n);<\/pre>\n
    \u6570\u7ec4<\/strong><\/div>\n

    JSX \u5141\u8bb8\u5728\u6a21\u677f\u4e2d\u63d2\u5165\u6570\u7ec4\uff0c\u6570\u7ec4\u4f1a\u81ea\u52a8\u5c55\u5f00\u6240\u6709\u6210\u5458\uff1a<\/p>\n

    React \u5b9e\u4f8b<\/p>\n

    \r\nvar arr = [\r\n  <h1>Linux\u5c31\u8be5\u8fd9\u4e48\u5b66<\/h1>,\r\n  <h2>\u5b66\u7684\u4e0d\u4ec5\u662f\u6280\u672f\uff0c\u66f4\u662f\u68a6\u60f3\uff01<\/h2>,\r\n];\r\nReactDOM.render(\r\n  <div>{arr}<\/div>,\r\n  document.getElementById('example')\r\n);<\/pre>\n","protected":false},"excerpt":{"rendered":"

    React \u4f7f\u7528 JSX \u6765\u66ff\u4ee3\u5e38\u89c4\u7684 JavaScript\u3002 JSX \u662f\u4e00\u4e2a\u770b\u8d77\u6765\u5f88\u50cf XML \u7684 Java […]<\/p>\n","protected":false},"author":1470,"featured_media":207359,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-207518","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=207518"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207518\/revisions"}],"predecessor-version":[{"id":207521,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/207518\/revisions\/207521"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/207359"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=207518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=207518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=207518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}