{"id":219002,"date":"2021-05-30T09:00:40","date_gmt":"2021-05-30T01:00:40","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=219002"},"modified":"2021-05-22T22:11:57","modified_gmt":"2021-05-22T14:11:57","slug":"upgrade-react17","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/upgrade-react17.html","title":{"rendered":"\u5347\u7ea7\u5230React 17\u4e4b\u524d\u5e94\u8be5\u77e5\u9053\u7684\u4e8b\u60c5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u5c06\u8ba8\u8bbaJSX\u7684\u6539\u8fdb\uff0c\u5b83\u4eec\u80cc\u540e\u7684\u52a8\u673a\uff0c\u4ee5\u53ca\u6bcf\u4e2a\u5f00\u53d1\u8005\u5728\u5347\u7ea7\u5230React 17\u4e4b\u524d\u5e94\u8be5\u77e5\u9053\u7684\u4e8b\u60c5\u3002\u4eca\u5929\u5206\u4eab\u7684\u5185\u5bb9\u662fReact 17\u4e2dJSX\u7684\u65b0\u7279\u6027\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u6b63\u6587<\/strong><\/div>\n

\u672c\u6587\u4e3b\u8981\u4ecb\u7ecdReact 17\u4e2dJSX\u7684\u65b0\u7279\u6027\u4ee5\u53ca\u4f60\u4e3a\u4ec0\u4e48\u8981\u5173\u6ce8\u5b83\u3002<\/p>\n

React 17\u7684\u53d1\u5e03\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002\u51e0\u4e4e\u6ca1\u6709\u4efb\u4f55\u4f60\u4f1a\u6ce8\u610f\u5230\u7684\u65b0\u529f\u80fd\u3002\u7136\u800c\uff0c\u4f60\u53ef\u4ee5\u627e\u5230\u51e0\u4e2a\u5bf9React\u6838\u5fc3\u5f71\u54cd\u5f88\u5927\u7684\u6539\u8fdb\u3002\u5728\u8fd9\u4e9b\u6539\u8fdb\u4e2d\uff0cJJSX\u7684\u53d8\u5316\u662f\u975e\u5e38\u660e\u663e\u7684\u3002<\/p>\n

\u672c\u6587\u5c06\u8ba8\u8bbaJSX\u7684\u6539\u8fdb\uff0c\u5b83\u4eec\u80cc\u540e\u7684\u52a8\u673a\uff0c\u4ee5\u53ca\u6bcf\u4e2a\u5f00\u53d1\u8005\u5728\u5347\u7ea7\u5230React 17\u4e4b\u524d\u5e94\u8be5\u77e5\u9053\u7684\u4e8b\u60c5\u3002<\/p>\n

\u9996\u5148\uff0c\u6700\u91cd\u8981\u7684\u662f\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u4f60\u5c06\u4ece\u5916\u90e8\u4f53\u9a8c\u5230\u7684\u53d8\u5316\u3002<\/p>\n

\u5bf9\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u53ef\u89c1\u7684\u53d8\u5316<\/strong><\/div>\n

\u5bf9React 17\u7684\u80a4\u6d45\u7814\u7a76\u80af\u5b9a\u4f1a\u8ba9\u4f60\u5370\u8c61\u4e0d\u6df1\u3002\u771f\u6b63\u4ee4\u4eba\u5174\u594b\u7684\u4e1c\u897f\u4e0d\u662f\u65b0\u529f\u80fd\uff0c\u800c\u662f\u5728React\u7684\u7f16\u8bd1\u65b9\u5f0f\u3002<\/p>\n

\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u4e9b\uff0c\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u4f7f\u7528\u65e7\u7248React\u7684\u7ec4\u4ef6\u4e2d\u7684JSX\u7684\u7f16\u8bd1\u4ee3\u7801\u3002<\/p>\n

\"\"<\/p>\n

\u4f60\u53ef\u80fd\u4f1a\u6ce8\u610f\u5230\uff0c\u7f16\u8bd1\u540e\u7684\u7248\u672c\u4f7f\u7528React.createElement\uff0c\u5176\u4e2dReact\u7684\u4f9d\u8d56\u6027\u5e94\u8be5\u5728\u8303\u56f4\u5185\u53ef\u7528\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u4f60\u9700\u8981\u5728\u6bcf\u4e2a\u7ec4\u4ef6\u4e2d\u9996\u5148\u5bfc\u5165React\u3002<\/p>\n

\u73b0\u5728\u8ba9\u6211\u4eec\u6765\u770b\u770b\u5b83\u5728React 17\u4e2d\u662f\u5982\u4f55\u5de5\u4f5c\u7684\u3002<\/p>\n

\u6709\u4e86React 17\uff0c\u4f60\u5c31\u4e0d\u9700\u8981\u4e3aJSX\u5bfc\u5165React\u4e86\u3002<\/p>\n

\u6211\u5e0c\u671b\u8fd9\u80fd\u63d0\u4f9b\u4e00\u4e2a\u7ebf\u7d22\uff0c\u5373\u7f16\u8bd1\u540e\u7684\u7248\u672c\u4e0d\u9700\u8981React\u7684\u5bfc\u5165\u3002<\/p>\n

\u6b63\u5982\u4f60\u5728\u4e0b\u56fe\u4e2d\u770b\u5230\u7684\uff0cReact 17\u7f16\u8bd1\u5668\u4ecereact\/jsx-runtime\u5bfc\u5165\u4e86\u4e00\u4e2a\u65b0\u7684\u4f9d\u8d56\u9879\uff0c\u5b83\u5904\u7406JJSX\u8f6c\u6362\u3002<\/p>\n

\"\"<\/p>\n

\u56e0\u6b64\uff0c\u4f5c\u4e3a\u5f00\u53d1\u8005\uff0c\u4e00\u65e6\u4f60\u5347\u7ea7\u5230React 17\uff0c\u4f60\u53ef\u4ee5\u4ece\u4f60\u7684\u7ec4\u4ef6\u7684\u4ee3\u7801\u4e2d\u5220\u9664React\u5bfc\u5165\uff0c\u5982\u679c\u5b83\u53ea\u662f\u4e3a\u4e86JSX\u800c\u5b58\u5728\u3002<\/p>\n

\u4f46\u8fd9\u662f\u552f\u4e00\u7684\u53d8\u5316\u5417\uff0c\u6709\u5173\u7cfb\u5417?<\/strong><\/div>\n

\u6b63\u5982\u4f60\u5df2\u7ecf\u6ce8\u610f\u5230\u7684\uff0c\u4ece\u5916\u9762\u770b\u6548\u679c\u4f3c\u4e4e\u65e0\u5173\u7d27\u8981\u3002<\/p>\n

\u91cd\u8981\u7684\u662f\u8981\u660e\u767d\uff0c\u5f53\u4f60\u7684\u4ee3\u7801\u5e93\u4e2d\u6709\u66f4\u591a\u7684React\u7ec4\u4ef6\u65f6\uff0c\u6574\u4f53\u6548\u679c\u4f1a\u663e\u73b0\u51fa\u6765<\/p>\n

\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u8fd9\u79cd\u5f71\u54cd\uff0c\u8ba9\u6211\u4eec\u770b\u770b\u4e3a\u4ec0\u4e48\u53d6\u6d88\u4e0eReact.createElement\u7684\u4f9d\u8d56\u5173\u7cfb\u5bf9JSX\u5f88\u91cd\u8981\u3002<\/p>\n

\u5220\u9664createElement\u7684\u597d\u5904<\/strong><\/div>\n

\u9996\u5148\u505a\u4e2a\u603b\u7ed3\u7684\u8bdd\uff0c\u6709\u51e0\u4e2a\u70b9:<\/p>\n

    \n\u51cf\u5c11\u6346\u7ed1\u6587\u4ef6\u7684\u5927\u5c0f<\/ol>\n
      \u51cf\u5c11\u52a8\u6001\u5c5e\u6027\u67e5\u8be2<\/ol>\n
        Props\u3001Args\u548cKeyRef\u76f8\u5173\u7684\u6539\u8fdb<\/ol>\n
        \u51cf\u5c11\u6346\u7ed1\u6587\u4ef6\u7684\u5927\u5c0f<\/strong><\/span><\/div>\n

        \u9996\u5148\uff0c\u53ef\u4ee5\u60f3\u5230\u7684\u4e00\u70b9\u662f\uff1a\u51cf\u5c11\u6346\u7ed1\u6587\u4ef6\u7684\u5927\u5c0f\u3002<\/p>\n

        \u968f\u7740React\u5bfc\u5165\u7684\u5220\u9664\uff0c\u4f60\u7684\u7f16\u8bd1\u6346\u7ed1\u8f93\u51fa\u7684\u5927\u5c0f\u5c06\u53d8\u5f97\u7a0d\u5fae\u5c0f\u4e00\u4e9b\u3002\u6211\u5e0c\u671b\u8fd9\u4e00\u70b9\u53d8\u5f97\u5f88\u660e\u663e\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u5728\u7f16\u8bd1\u5668\u5c06\u5176\u66ff\u6362\u4e3aReact\u4e2d\u7684\u5b50\u6a21\u5757\u7684\u6bcf\u4e2a\u7ec4\u4ef6\u4e2d\u5220\u9664React\u5bfc\u5165\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n

        import {jsx as _jsx} from 'react\/jsx-runtime'; <\/pre>\n
        \u51cf\u5c11\u52a8\u6001\u5c5e\u6027\u67e5\u8be2<\/strong><\/span><\/div>\n

        \u7531\u4e8eReact 17\u4e0d\u518d\u4e3aJSX\u4f7f\u7528React.createElement\uff0c\u56e0\u6b64\u6d88\u9664\u4e86\u5bf9\u52a8\u6001\u5c5e\u6027\u67e5\u627e\u7684\u9700\u8981\u3002\u6b63\u5982\u524d\u9762\u6240\u8ba8\u8bba\u7684\uff0c\u4f60\u53ef\u4ee5\u5728\u524d\u7aef\u4ee3\u7801\u5e93\u7684\u7f16\u8bd1\u7248\u672c\u4e2d\u627e\u5230\u8fd9\u4e2a\u3002<\/p>\n

        \u7136\u800c\uff0c\u8fd9\u91cc\u7684\u6027\u80fd\u6539\u8fdb\u662f\u975e\u5e38\u5c0f\u7684\uff0c\u5728\u8fd9\u91cc\u4f60\u51e0\u4e4e\u4e0d\u4f1a\u6ce8\u610f\u5230\u5dee\u5f02 \u8fd9\u662f\u56e0\u4e3a\u73b0\u4ee3\u7684JavaScript\u5f15\u64ce\u4e3b\u8981\u662f\u9488\u5bf9\u52a8\u6001\u5c5e\u6027\u67e5\u8be2\u8fdb\u884c\u4f18\u5316\u7684\u3002<\/p>\n

        Props\u3001Args\u548cKeyRef\u76f8\u5173\u7684\u6539\u8fdb<\/strong><\/span><\/div>\n

        \u597d\u5427\uff0c\u6211\u53ea\u662f\u60f3\u544a\u8bc9\u4f60\uff0c\u8fd9\u4e9b\u6539\u8fdb\u5b58\u5728\u4e8eReact 17\u4e2d\u3002\u7136\u800c\uff0c\u8fd9\u4e9b\u6539\u8fdb\u592a\u8fc7\u6280\u672f\u6027\uff0c\u65e0\u6cd5\u8be6\u8ff0\u3002\u5982\u679c\u4f60\u6709\u5174\u8da3\uff0c\u4f60\u53ef\u4ee5\u5728Motivation\u90e8\u5206\u7684create-element-changes\u4e2d\u9605\u8bfb\u5b83\u4eec\u3002<\/p>\n

        \u9700\u8981\u7528React17\u5417<\/strong><\/div>\n

        \u5982\u679c\u4f60\u5728JSX\u4e4b\u5916\u521b\u5efa\u52a8\u6001\u5143\u7d20\uff0c\u4f60\u4ecd\u7136\u9700\u8981React.createElement\u65b9\u6cd5\u3002<\/p>\n

        \u5982\u679c\u4f60\u9700\u8981\u5728\u4f60\u7684\u4ee3\u7801\u4e2d\u624b\u52a8\u521b\u5efa\u5143\u7d20\uff0c\u4f60\u5e94\u8be5\u7ee7\u7eed\u4f7f\u7528React.createElement\u3002<\/p>\n

        \u6b64\u5916\uff0c\u4f60\u53ef\u80fd\u4f1a\u60f3\uff0c\u6211\u4eec\u4e0d\u662f\u8fd8\u5728\u4f7f\u7528react\/jsx-runtime\u6765\u66ff\u4ee3React 17\u7684React.createElement\u7684JSX\u5417\uff0c\u8fd9\u4e9b\u95ee\u9898\u5728\u90a3\u91cc\u5f97\u5230\u4e86\u89e3\u51b3\u5417?\u7b80\u800c\u8a00\u4e4b\uff0c\u662f\u7684!<\/p>\n

        \u5f53\u6211\u4eec\u770b\u65b0\u7684react\/jsx-runtime\u65f6\uff0c\u5b83\u5e26\u6765\u4e86\u4e00\u4e9b\u8bbe\u8ba1\u4e0a\u7684\u53d8\u5316\uff0c\u4ee5\u907f\u514dReact.createElement\u65b9\u6cd5\u4e2d\u51fa\u73b0\u7684\u74f6\u9888\u95ee\u9898\u3002<\/p>\n

        \u5176\u4e2d\u4e00\u4e9b\u53d8\u5316\u7684\u5f15\u5165\u662f\u4e3a\u4e86\u5728\u672a\u6765\u7684\u67b6\u6784\u4e2d\u6709\u6240\u53d1\u5c55\u3002<\/p>\n

        \u4f60\u53ef\u4ee5\u5728\u8be6\u7ec6\u8bbe\u8ba1\u90e8\u5206\u4e0b\u7684\u540c\u4e00\u94fe\u63a5create-element-changes\u4e2d\u9605\u8bfb\u8fd9\u4e9b\u5185\u5bb9\u3002\u94fe\u63a5:<\/p>\n

        https:\/\/github.com\/reactjs\/rfcs\/blob\/createlement-rfc\/text\/0000-create-element-changes.md#motivation<\/p>\n

        \u4f60\u8fd8\u5e94\u8be5\u77e5\u9053\u4ec0\u4e48?<\/strong><\/div>\n

        \u55ef\uff0c\u8fd9\u4e0d\u662f\u522b\u7684\uff0c\u800c\u662f\u5e9f\u5f03\u901a\u77e5\u3002<\/p>\n

        \u5728React 17 JSX\u7684\u53d8\u5316\u4e2d\uff0c\u6709\u51e0\u4e2a\u901a\u77e5\u662f\u4f60\u5e94\u8be5\u6ce8\u610f\u7684\u3002<\/p>\n

        \u5e9f\u9664 \"\u6a21\u5757\u6a21\u5f0f \"\u7ec4\u4ef6<\/strong><\/span><\/div>\n
        const Foo = (props) => { \r\n  return { \r\n    onClick() { \r\n      \/\/... \r\n    } \r\n    render() { \r\n      return 
        <\/div>; \r\n } \r\n } \r\n}; <\/pre>\n

        \u7136\u800c\uff0c\u901a\u8fc7\u505a\u4e0b\u9762\u63d0\u5230\u7684\u4e24\u4e2a\u6539\u53d8\uff0c\u5347\u7ea7\u4f1a\u66f4\u5bb9\u6613\u3002<\/p>\n

          \u4f7f\u7528\u51fd\u6570\u8868\u8fbe\u5f0f\u800c\u4e0d\u662f\u7bad\u5934\u51fd\u6570\u3002<\/ol>\n
            \u6dfb\u52a0\u4e00\u4e2a\u5e26\u6709isReactComponent\u7684\u539f\u578b\uff0c\u544a\u8bc9React\u533a\u5206\u7c7b\u548c\u51fd\u6570\u7ec4\u4ef6<\/ol>\n

            \u7ed3\u679c\u770b\u8d77\u6765\u5982\u4e0b\u3002<\/p>\n

            function Foo(props) { \r\n  return { \r\n    onClick() { \r\n      \/\/... \r\n    } \r\n    render() { \r\n      return 
            <\/div>; \r\n } \r\n } \r\n}; \r\nFoo.prototype = { isReactComponent: true}; <\/pre>\n

            \u540c\u6837\uff0c\u4e5f\u4f1a\u6709\u4ee5\u4e0b\u7684\u5e9f\u5f03\u901a\u77e5\u3002<\/p>\n

              \u5e9f\u5f03\u51fd\u6570\u7ec4\u4ef6\u4e0a\u7684defaultProps\u3002<\/ol>\n
                \u5e9f\u5f03\u5bf9\u8c61\u4e2d\u7684spreading key\u3002<\/ol>\n
                  \u5e9f\u5f03\u5b57\u7b26\u4e32\u5f15\u7528(\u5e76\u5220\u9664\u751f\u4ea7\u6a21\u5f0f_\u6240\u6709\u8005\u5b57\u6bb5)\u3002<\/ol>\n

                  \u4f60\u53ef\u4ee5\u5728\u8be6\u7ec6\u8bbe\u8ba1\u90e8\u5206\u7684\u540c\u4e00\u4e2a\u94fe\u63a5create-element-changes\u4e2d\u8bfb\u5230\u5b83\u4eec\uff0c\u94fe\u63a5\uff1a<\/p>\n

                  https:\/\/github.com\/reactjs\/rfcs\/blob\/createlement-rfc\/text\/0000-create-element-changes.md#motivation<\/p>\n

                  \u7136\u800c\uff0c\u8fd9\u4e9b\u8b66\u544a\u5e76\u4e0d\u59a8\u788d\u4f60\u8fc1\u79fb\u5230React 17\u3002\u5728\u4e0b\u4e00\u4e2a\u4e3b\u8981\u7248\u672c\u4e4b\u524d\uff0c\u4f60\u4e5f\u6709\u65f6\u95f4\u6765\u5347\u7ea7\u5b83\u4eec\u3002<\/p>\n

                  \u603b\u7ed3<\/strong><\/div>\n

                  JSX\u53d8\u6362\u7684\u597d\u6d88\u606f\u662f\uff0c\u5b83\u662f\u5411\u540e\u517c\u5bb9\u7684\uff0c\u5347\u7ea7\u5230React 17\u7684\u53d8\u5316\u5f88\u5c0f\u3002<\/p>\n

                  \u4f46\u6211\u89c9\u5f97\u5f71\u54cd\u53ef\u80fd\u4f1a\u66f4\u597d\uff0c\u56e0\u4e3a\u76ee\u524d\u7684\u6539\u8fdb\u5728\u73b0\u5b9e\u4e16\u754c\u4e2d\u51e0\u4e4e\u770b\u4e0d\u5230\u3002<\/p>\n

                  \u7136\u800c\uff0c\u540c\u6837\u91cd\u8981\u7684\u662f\u8981\u660e\u767d\uff0c\u8fd9\u4e9b\u6539\u8fdb\u4e2d\u7684\u5927\u90e8\u5206\u5c06\u6709\u52a9\u4e8e\u52a0\u901f\u672a\u6765\u7684\u53d1\u5e03\u3002\u56e0\u6b64\uff0c\u5347\u7ea7\u5230React 17\u5c06\u4f7f\u4f60\u7684\u4ee3\u7801\u5e93\u9762\u5411\u672a\u6765\u3002\u800c\u4e14\u4f60\u4e0d\u9700\u8981\u63d0\u9192\u81ea\u5df1\u5728JSX\u6587\u4ef6\u4e2d\u4fdd\u7559React\u5bfc\u5165\u3002<\/p>\n

                  \u6b64\u5916\uff0c\u5982\u679c\u4f60\u4ece\u65e7\u7248\u672c\u5347\u7ea7\uff0c\u503c\u5f97\u53c2\u8003\u5e9f\u6b62\u901a\u77e5\uff0c\u4ee5\u907f\u514d\u5c06\u6765\u51fa\u73b0\u4efb\u4f55\u9ebb\u70e6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

                  \u672c\u6587\u4e3b\u8981\u4ecb\u7ecdReact 17\u4e2dJSX\u7684\u65b0\u7279\u6027\u4ee5\u53ca\u4f60\u4e3a\u4ec0\u4e48\u8981\u5173\u6ce8\u5b83\u3002 React 17\u7684\u53d1\u5e03\u662f\u72ec\u4e00\u65e0\u4e8c\u7684\u3002\u51e0\u4e4e\u6ca1 […]<\/p>\n","protected":false},"author":1920,"featured_media":119570,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[21],"tags":[],"class_list":["post-219002","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-news"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/219002","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\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=219002"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/219002\/revisions"}],"predecessor-version":[{"id":219117,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/219002\/revisions\/219117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/119570"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=219002"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=219002"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=219002"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}