{"id":81971,"date":"2023-11-09T13:45:58","date_gmt":"2023-11-09T05:45:58","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=81971"},"modified":"2023-11-09T13:45:58","modified_gmt":"2023-11-09T05:45:58","slug":"react-protect","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/react-protect.html","title":{"rendered":"\u6d45\u8c08React\u524d\u540e\u7aef\u540c\u6784\u9632\u6b62\u91cd\u590d\u6e32\u67d3"},"content":{"rendered":"

\u9996\u5148\u89e3\u91caReact\u524d\u540e\u7aef\u540c\u6784\u3001React\u9996\u5c4f\u6e32\u67d3\u7684\u6982\u5ff5\u3002\u7136\u540e\u901a\u8fc7\u8fd92\u4e2a\u6982\u5ff5\u89e3\u51b3\u670d\u52a1\u7aef\u6e32\u67d3\u5b8c\u6210\u540e\u6d4f\u89c8\u5668\u7aef\u91cd\u590d\u6e32\u67d3\u7684\u95ee\u9898\u3002<\/p>\n

\u4ec0\u4e48\u53eb\u524d\u540e\u7aef\u540c\u6784\uff1f <\/strong><\/div>\n

\u4e3a\u4e86\u89e3\u51b3\u67d0\u4e9b\u95ee\u9898\uff08\u6bd4\u5982SEO\u3001\u63d0\u5347\u6e32\u67d3\u901f\u5ea6\u7b49\uff09react<\/em><\/strong> \u63d0\u4f9b\u4e862\u4e2a\u65b9\u6cd5\u5728\u670d\u52a1\u7aef\u751f\u6210\u4e00\u4e2aHTML\u6587\u672c\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u5728\u5f97\u5230\u4e86\u8fd9\u4e2aHTML\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u4e4b\u540e\uff0c\u901a\u5e38\u4f1a\u5c06\u5176\u7ec4\u88c5\u6210\u4e00\u4e2a\u9875\u9762\u76f4\u63a5\u8fd4\u56de\u7ed9\u7528\u6237\u7684\u6d4f\u89c8\u5668\u3002<\/p>\n

\u5230\u8fd9\u91cc\uff0c\u670d\u52a1\u7aef\u7684\u6d3b\u5df2\u7ecf\u5e72\u5b8c\u4e86\uff0c\u7136\u540e\u5c31\u662f\u6d4f\u89c8\u5668\u8fd9\u8fb9\u5e72\u6d3b\u3002<\/p>\n

\u6d4f\u89c8\u5668\u62ff\u5230HTML\u6587\u672c\u540e\uff0c\u7acb\u523b\u8fdb\u884c\u6e32\u67d3\u5c06\u5185\u5bb9\u5448\u73b0\u7ed9\u7528\u6237\u3002\u7136\u540e\u52a0\u8f7d\u9875\u9762\u6240\u9700\u7684 .js \u6587\u4ef6\uff0c\u7136\u540e\u6267\u884cJavaScript\u00a0<\/strong><\/em>\u811a\u672c\uff0c\u7136\u540e\u5f00\u59cb\u521d\u59cb\u5316\u00a0react<\/strong><\/em> \u7ec4\u4ef6\u2026\u2026\u2026\u2026<\/p>\n

\u5230\u8fd9\u91cc\u95ee\u9898\u5c31\u6765\u4e86\u3002react<\/em><\/strong> \u521d\u59cb\u5316\u7ec4 \u4ef6\u540e\u4f1a\u6267\u884c\u7ec4\u4ef6\u5185\u6240\u6709 render\u00a0() <\/em>\u65b9\u6cd5\uff0c\u7136\u540e\u751f\u6210\u865a\u62dfDOM\u7684\u6811\u5f62\u7ed3\u6784\uff0c\u7136\u540e\u5728\u9002\u5f53\u7684\u65f6\u5019\u5c06\u865a\u62dfdom\u5199<\/em>\u5230\u6d4f\u89c8\u5668\u7684\u771f\u5b9edom\u4e2d\u3002\u56e0\u4e3a react<\/em><\/strong> \u603b\u662f\u6839\u636e\u865a\u62dfdom\u6765\u751f\u6210\u771f\u5b9edom\uff0c\u6240\u4ee5\u6700\u540e\u4f1a\u628a\u670d\u52a1\u5668\u7aef\u6e32\u67d3\u597d\u7684HTML\u5168\u90e8\u66ff\u6362\u6389\u3002<\/p>\n

\u4e0a\u9762\u8fd9\u4e2a\u4e8b\u60c5\u8bf4\u4e0d\u662f\u95ee\u9898\u786e\u5b9e\u4e5f\u4e0d\u662f\u95ee\u9898\uff0c\u65e0\u975e\u5c31\u662f\u7528\u6237\u770b\u5230\u9875\u9762\u7136\u540e\u201c\u95ea\u73b0\u201d\u4e00\u4e0b\u3002\u8bf4\u662f\u95ee\u9898\u8fd8\u771f\u662f\u4e2a\u95ee\u9898\uff0c\u4ea7\u54c1\u4f1a\u62ff\u7740\u8fd9\u6bdb\u75c5\u4ece\u7528\u6237\u4f53\u9a8c\u7684\u89d2\u5ea6\u5728\u5404\u79cd\u573a\u5408\u548c\u4f60\u6b7b\u78d5\u534a\u4e2a\u6708\u3002\u78d5\u7d2f\u4e86\u4f60\u7d22\u6027\u628a\u670d\u52a1\u7aef\u6e32\u67d3\u5173\u4e86\uff0c\u7136\u540e\u8fd0\u8425\u53c8\u62ff\u7740SEO\u7684\u95ee\u9898\u51c6\u5907\u548c\u4f60\u5f00\u59cb\u6495\u903c\u4e86\u3002<\/p>\n

\u806a\u660e\u5982 Facebook \u7684\u5de5\u7a0b\u5e08\u5f53\u7136\u60f3\u5230\u4e86\u8fd9\u4e9b\u95ee\u9898\uff0c\u6240\u4ee5\u4ed6\u4eec\u5728ReactDOMServer.renderToString(element) \u65b9\u6cd5<\/em>\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2a checksum<\/em><\/strong> \u673a\u5236\u3002<\/p>\n

\u5173\u4e8e\u00a0checksum <\/em><\/strong>\u5b98\u7f51 \u5e76\u6ca1\u6709\u592a\u591a\u4ecb\u7ecd\uff0c\u4f46\u662f\u56fd\u5185\u5916\u7684\u5404\u8def\u535a\u5ba2\u4ecb\u7ecd\u4e86\u4e0d\u5c11\u3002\u6211\u4e00\u76f4\u60f3\u627e\u00a0react<\/strong><\/em> \u5f00\u53d1\u8005\u5173\u4e8e\u8fd9\u4e2a\u673a\u5236\u7684\u4ecb\u7ecd\u4e00\u76f4\u6ca1\u627e\u5230\u2026\u2026\u3002<\/p>\n

\u524d\u540e\u7aef\u540c\u6784<\/strong>\u5c31\u662f\u4fdd\u8bc1\u524d\u7aef\u548c\u540e\u7aef\u7684dom\u7ed3\u6784\u4e00\u81f4\uff0c\u4e0d\u4f1a\u53d1\u751f\u91cd\u590d\u6e32\u67d3\u3002react<\/strong><\/em> \u4f7f\u7528\u00a0checksum <\/em><\/strong>\u673a\u5236\u8fdb\u884c\u4fdd\u969c\u3002<\/p>\n

\u4ec0\u4e48\u53ebReact\u9996\u5c4f\u6e32\u67d3\uff1f<\/strong><\/div>\n

\u7b80\u5355\u7684\u8bf4\u5c31\u662f react<\/strong><\/em> \u5728\u6d4f\u89c8\u5668\u5185\u5b58\u4e2d\u7b2c\u4e00\u6b21\u751f\u6210\u7684\u865a\u62df dom \u6811\u3002\u5207\u8bb0\u662f\u865a\u62df dom \uff0c\u800c\u4e0d\u662f\u6d4f\u89c8\u5668\u7684dom<\/strong>\u3002<\/p>\n

\u4e86\u89e3 react<\/em><\/strong> \u7684\u5e94\u8be5\u77e5\u9053\uff0c\u6240\u6709 react<\/strong><\/em> \u7ec4\u4ef6\u90fd\u6709\u4e00\u4e2a render()<\/em> \u65b9\u6cd5\uff08\u5982\u679c\u4f7f\u7528function\u65b9\u5f0f\u7f16\u5199\u7684\u7ec4\u4ef6\u4f1a\u628afunction\u91cc\u7684\u6240\u6709\u4ee3\u7801\u90fd\u585e\u5230 render()<\/em> \u65b9\u6cd5\u4e2d\u53bb\uff09\u3002\u5f53ReactDOM.render( element, container, [callback] )<\/em>\u65b9\u6cd5\u6267\u884c\u65f6\uff0c\u4f1a\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n

    \n
  1. \u6240\u6709\u7ec4\u4ef6\u7684\u4f1a\u5148\u8fdb\u884c\u521d\u59cb\u5316\uff08es6\u6267\u884c\u6784\u9020\u51fd\u6570\uff09\u3002<\/li>\n
  2. \u6240\u6709\u7ec4\u4ef6\u7684\u00a0render<\/em>\u00a0()<\/em> \u65b9\u6cd5\u4f1a\u88ab\u8c03\u7528\u4e00\u6b21\uff0c\u5b8c\u6210\u8fd9\u4e2a\u8fc7\u7a0b\u540e\u4f1a\u5f97\u5230\u4e00\u9897\u865a\u62df\u7684 dom \u6811\u3002<\/li>\n
  3. \u00a0react<\/strong><\/em> \u4f1a\u5c06\u865a\u62dfdom\u8f6c\u6362\u6210\u6d4f\u89c8\u5668dom\uff0c\u5b8c\u6210\u540e\u8c03\u7528\u7ec4\u4ef6\u7684\u00a0componentDidMount()<\/em>\u00a0\u65b9\u6cd5\u544a\u8bc9\u4f60\u5df2\u7ecf\u88c5\u8f7d\u5230\u6d4f\u89c8\u5668\u4e0a\u4e86\u3002<\/li>\n<\/ol>\n

    \u5728\u4e0a\u9762\u8fd9\u4e2a\u8fc7\u7a0b\u6210\u4e2d\uff0c\u6b65\u9aa42\u5b8c\u6210\u540e\u5373\u4e3a\u5b8c\u6210 react<\/strong><\/em> \u7684\u9996\u5c4f\u6e32\u67d3\u3002\u7ed3\u5408 checksum<\/em><\/strong>\u00a0\u673a\u5236\u6b65\u9aa43\u6709\u53ef\u80fd\u4e0d\u4f1a\u6267\u884c\u3002<\/p>\n

    \u5f53\u7ec4\u4ef6\u72b6\u6001\u53d1\u751f\u53d8\u66f4\u65f6\uff08 setState() <\/em>\u751f\u547d\u5468\u671f\u51fd\u6570\u88ab\u8c03\u7528\uff09\u6216\u8005 \u7236\u7ec4\u4ef6\u6e32\u67d3\u65f6\uff08\u7236\u7ec4\u4ef6\u7684 render()<\/em> \u65b9\u6cd5\u88ab\u8c03\u7528\uff09\uff0c\u5f53\u524d\u7ec4\u4ef6\u7684 render()<\/em> \u65b9\u6cd5\u90fd\u4f1a\u88ab\u6267\u884c\uff0c\u90fd\u6709\u53ef\u80fd\u4f1a\u5bfc\u81f4\u865a\u62dfdom\u53d8\u66f4\uff0c\u4f46\u662f\u8fd9\u4e9b\u53d8\u66f4\u548c\u9996\u5c4f\u6e32\u67d3\u6ca1\u4efb\u4f55\u5173\u7cfb\u4e86\u3002<\/p>\n

    React\u524d\u540e\u7aef\u540c\u6784\u9996\u5c4f\u6e32\u67d3<\/strong><\/div>\n

    \u4e86\u89e3\u4e86\u540c\u6784\u548c\u9996\u5c4f\u6e32\u67d3\uff0c\u5c31\u597d\u7406\u89e3\u5982\u4f55\u89e3\u51b3\u9996\u5c4f\u4e0d\u91cd\u590d\u6e32\u67d3\u7684\u95ee\u9898\u4e86\u3002<\/p>\n

    \u9996\u5148\u670d\u52a1\u7aef\u6e32\u67d3\u5b8c\u4e4b\u540e\u4f1a\u6709\u4e00\u4e2a checksum<\/strong><\/em> \u503c\u5199\u5728\u6839\u5143\u7d20\u7684\u5c5e\u6027\u4e0a\uff1a<\/p>\n

    \"\"<\/p>\n

    \u8fd9\u4e2a checksum<\/strong><\/em>\u00a0\u662f\u6839\u636e\u670d\u52a1\u7aef\u751f\u6210\u7684HTML\u5185\u5bb9\u54c8\u5e0c\u8ba1\u7b97\u5f97\u5230\u7684\u3002<\/p>\n

    \u7136\u540e\u5728\u6d4f\u89c8\u5668\u52a0\u8f7d\u5b8c\u6240\u6709\u7684js\u6587\u4ef6\u4e4b\u540e\uff0c\u5f00\u59cb\u6267\u884c\u524d\u9762\u4ecb\u7ecd\u7684\u00a0ReactDOM.render( element, container, [callback] )<\/em>\u00a0\u521d\u59cb\u5316\u6e32\u67d3\u7684\u4e09\u4e2a\u6b65\u9aa4\u3002\u5f53\u6267\u884c\u5b8c\u7b2c\u4e8c\u6b65\u751f\u6210\u865a\u62dfdom\u540e\uff0creact<\/em><\/strong> \u4f1a\u6839\u865a\u62dfdom\u7528\u76f8\u540c\u7684\u7b97\u6cd5\u8ba1\u7b97\u4e00\u4e2a\u54c8\u5e0c\u503c\uff0c\u5982\u679c\u548c checksum<\/strong><\/em> \u4e00\u81f4\u5219\u8ba4\u4e3a\u670d\u52a1\u5668\u5df2\u7ecf\u5b8c\u6210\u6e32\u67d3\uff0c\u4e0d\u4f1a\u518d\u6267\u884c\u7b2c\u4e09\u6b65\u3002<\/p>\n

    \u5982\u679c checksum<\/em><\/strong> \u6bd4\u5bf9\u4e0d\u4e00\u81f4\uff0c\u5728 \u5f00\u53d1\u73af\u5883<\/strong>\u00a0\u548c \u6d4b\u8bd5\u73af\u5883<\/strong> \u4f1a\u5728\u6d4f\u89c8\u5668console\u4e2d\u8f93\u51fa\u4ee5\u4e0b\u8b66\u544a\u5185\u5bb9\uff1a<\/p>\n

    \"\"<\/p>\n

    \u751f\u4ea7\u73af\u5883\u4e0d\u4f1a\u8f93\u51fa\u4efb\u4f55\u8b66\u544a\u3002<\/strong><\/span><\/div>\n

    \u540c\u6784\u6e32\u67d3\u7684\u5185\u5bb9\u5c31\u8fd9\u4e48\u591a\uff0c\u539f\u7406\u5176\u5b9e\u86ee\u7b80\u5355\u7684\uff0c\u65e0\u975e\u5c31\u662f\u4fdd\u8bc1DOM\u4e00\u81f4\u3002\u4f46\u662f\u7ed3\u5408\u4ee3\u7801\u5206\u7247\u3001\u5f02\u6b65\u52a0\u8f7d\u3001\u670d\u52a1\u7aef\u8c03\u63a5\u53e3\u5f02\u6b65\u7ec4\u88c5\u6570\u636e\u7b49\u7b49\u529f\u80fd\u540e\uff0c\u5982\u4f55\u4fdd\u8bc1\u670d\u52a1\u7aef\u548c\u6d4f\u89c8\u5668\u7aef\u7b2c\u4e00\u6b21\u6e32\u67d3\u7684dom\u4e00\u81f4\u8fd8\u5f97\u82b1\u4e0d\u5c11\u529f\u592b\u3002\u4e0d\u8fc7\u539f\u7406\u6e05\u695a\u4e86\uff0c\u4e8b\u60c5\u603b\u80fd\u529e\u6210\u3002<\/p>\n

    \n

    \u539f\u6587\u6765\u81ea\uff1ahttps:\/\/my.oschina.net\/chkui\/blog\/1523410<\/a><\/p>\n

    \u672c\u6587\u5730\u5740\uff1a http:\/\/lrxjmw.cn\/react-protect.html<\/a>\u7f16\u8f91\u5458\uff1a\u90dd\u5927\u53d1\uff0c\u5ba1\u6838\u5458\uff1a\u9004\u589e\u5b9d<\/span><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"

    \u9996\u5148\u89e3\u91caReact\u524d\u540e\u7aef\u540c\u6784\u3001React\u9996\u5c4f\u6e32\u67d3\u7684\u6982\u5ff5\u3002\u7136\u540e\u901a\u8fc7\u8fd92\u4e2a\u6982\u5ff5\u89e3\u51b3\u670d\u52a1\u7aef\u6e32\u67d3\u5b8c\u6210\u540e\u6d4f\u89c8\u5668\u7aef\u91cd\u590d\u6e32\u67d3 […]<\/p>\n","protected":false},"author":63,"featured_media":81976,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[21],"tags":[],"class_list":["post-81971","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\/81971","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\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=81971"}],"version-history":[{"count":9,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/81971\/revisions"}],"predecessor-version":[{"id":286870,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/81971\/revisions\/286870"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/81976"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=81971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=81971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=81971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}