{"id":5929,"date":"2023-01-06T15:38:38","date_gmt":"2023-01-06T07:38:38","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5929"},"modified":"2023-01-06T15:38:38","modified_gmt":"2023-01-06T07:38:38","slug":"%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8react-js%e4%b8%ad%e7%9a%84%e9%ab%98%e9%98%b6%e7%b5%84%e4%bb%b6higher-ordercomponents%e9%80%b2%e8%a1%8c%e4%bb%a3%e7%a2%bc%e5%a4%8d%e7%94%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8react-js%e4%b8%ad%e7%9a%84%e9%ab%98%e9%98%b6%e7%b5%84%e4%bb%b6higher-ordercomponents%e9%80%b2%e8%a1%8c%e4%bb%a3%e7%a2%bc%e5%a4%8d%e7%94%a8\/","title":{"rendered":"\u5982\u4f55\u5229\u7528React.js\u4e2d\u7684\u9ad8\u9636\u7d44\u4ef6(Higher-OrderComponents)\u9032\u884c\u4ee3\u78bc\u590d\u7528"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, Higher-Order Components, \u4ee3\u7801\u590d\u7528\"><\/p>\n<h1>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528\u9ad8\u9636\u7ec4\u4ef6(Higher-Order Components)\u8fdb\u884c\u4ee3\u7801\u590d\u7528\uff1f<\/h1>\n<p>React \u662f\u4e00\u500b\u7528\u65bc\u69cb\u5efa\u7528\u6236\u754c\u9762\u7684 JavaScript \u5eab\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u69cb\u5efa\u9ad8\u6027\u80fd\u7684\u7528\u6236\u754c\u9762\u3002React \u63d0\u4f9b\u4e86\u4e00\u7a2e\u53ef\u4ee5\u901a\u904e\u4f7f\u7528\u9ad8\u9636\u7d44\u4ef6(Higher-Order Components)\u4f86\u5be6\u73fe\u4ee3\u78bc\u590d\u7528\u7684\u65b9\u6cd5\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f\u9ad8\u9636\u7d44\u4ef6\uff1f<\/h2>\n<p>\u9ad8\u9636\u7d44\u4ef6\u662f\u4e00\u7a2e\u51fd\u6578\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u7d44\u4ef6\u4f5c\u70ba\u53c3\u6578\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u7d44\u4ef6\u3002\u5b83\u53ef\u4ee5\u7528\u65bc\u5c01\u88dd\u73fe\u6709\u7d44\u4ef6\uff0c\u4ee5\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u6dfb\u52a0\u984d\u5916\u7684 props\u3001\u6dfb\u52a0\u984d\u5916\u7684\u884c\u70ba\u6216\u8005\u6539\u8b8a\u73fe\u6709\u7d44\u4ef6\u7684\u6e32\u67d3\u884c\u70ba\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528\u9ad8\u9636\u7d44\u4ef6\uff1f<\/h2>\n<p>\u4f7f\u7528\u9ad8\u9636\u7d44\u4ef6\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\u5275\u5efa\u4e00\u500b\u9ad8\u9636\u7d44\u4ef6\u51fd\u6578\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u7d44\u4ef6\u4f5c\u70ba\u53c3\u6578\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u7d44\u4ef6\u3002<\/li>\n<li>\u5728\u9ad8\u9636\u7d44\u4ef6\u51fd\u6578\u4e2d\uff0c\u5c01\u88dd\u73fe\u6709\u7d44\u4ef6\uff0c\u4ee5\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4f8b\u5982\u6dfb\u52a0\u984d\u5916\u7684 props\u3001\u6dfb\u52a0\u984d\u5916\u7684\u884c\u70ba\u6216\u8005\u6539\u8b8a\u73fe\u6709\u7d44\u4ef6\u7684\u6e32\u67d3\u884c\u70ba\u3002<\/li>\n<li>\u5c07\u9ad8\u9636\u7d44\u4ef6\u51fd\u6578\u50b3\u905e\u7d66 React \u7684 <code>React.createElement()<\/code> \u65b9\u6cd5\uff0c\u4ee5\u5275\u5efa\u65b0\u7684\u7d44\u4ef6\u3002<\/li>\n<\/ol>\n<h2>\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u9ad8\u9636\u7d44\u4ef6\u793a\u4f8b\uff0c\u5b83\u5c07\u4e00\u500b\u7d44\u4ef6\u7684 props \u6dfb\u52a0\u5230\u53e6\u4e00\u500b\u7d44\u4ef6\u4e2d\uff1a<\/p>\n<pre class=\"brush: javascript\">\n\/\/ \u9ad8\u9636\u7d44\u4ef6\u51fd\u6578\nconst withExtraProp = (WrappedComponent) => {\n    return class extends React.Component {\n        render() {\n            return <WrappedComponent extraProp=\"extraPropValue\" {...this.props} \/>;\n        }\n    }\n};\n\n\/\/ \u73fe\u6709\u7d44\u4ef6\nclass MyComponent extends React.Component {\n    render() {\n        return <div>MyComponent<\/div>;\n    }\n}\n\n\/\/ \u5275\u5efa\u65b0\u7684\u7d44\u4ef6\nconst MyComponentWithExtraProp = withExtraProp(MyComponent);\n\n\/\/ \u4f7f\u7528\u65b0\u7684\u7d44\u4ef6\nReactDOM.render(<MyComponentWithExtraProp \/>, document.getElementById('root'));\n<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u5011\u5275\u5efa\u4e86\u4e00\u500b\u9ad8\u9636\u7d44\u4ef6\u51fd\u6578 <code>withExtraProp<\/code>\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u7d44\u4ef6\u4f5c\u70ba\u53c3\u6578\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u7d44\u4ef6\uff0c\u65b0\u7684\u7d44\u4ef6\u5c07\u6dfb\u52a0\u4e00\u500b\u540d\u70ba <code>extraProp<\/code> \u7684 prop\uff0c\u5176\u503c\u70ba <code>extraPropValue<\/code>\u3002\u7136\u5f8c\uff0c\u6211\u5011\u5c07\u9ad8\u9636\u7d44\u4ef6\u51fd\u6578\u50b3\u905e\u7d66 React \u7684 <code>React.createElement()<\/code> \u65b9\u6cd5\uff0c\u4ee5\u5275\u5efa\u65b0\u7684\u7d44\u4ef6 <code>MyComponentWithExtraProp<\/code>\uff0c\u4e26\u5c07\u5176\u7528\u65bc\u6e32\u67d3\u3002<\/p>\n<p>\u4f7f\u7528\u9ad8\u9636\u7d44\u4ef6\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u5c01\u88dd\u73fe\u6709\u7d44\u4ef6\uff0c\u4ee5\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4e26\u5be6\u73fe\u4ee3\u78bc\u590d\u7528\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>React \u63d0\u4f9b\u4e86\u4e00\u7a2e\u53ef\u4ee5\u901a\u904e\u4f7f\u7528\u9ad8\u9636\u7d44\u4ef6\u4f86\u5be6\u73fe\u4ee3\u78bc\u590d\u7528\u7684\u65b9\u6cd5\u3002\u9ad8\u9636\u7d44\u4ef6\u662f\u4e00\u7a2e\u51fd\u6578\uff0c\u5b83\u63a5\u53d7\u4e00\u500b\u7d44\u4ef6\u4f5c\u70ba\u53c3\u6578\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u65b0\u7684\u7d44\u4ef6\u3002\u4f7f\u7528\u9ad8\u9636\u7d44\u4ef6\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u5c01\u88dd\u73fe\u6709\u7d44\u4ef6\uff0c\u4ee5\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\uff0c\u4e26\u5be6\u73fe\u4ee3\u78bc\u590d\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aReact.js\u4e2d\u7684\u9ad8\u9636\u7d44\u4ef6(Higher-OrderComponents)\u53ef\u4ee5\u6709\u6548\u5730\u5c07\u7d44\u4ef6\u7684\u5171\u540c\u529f\u80fd\u62bd\u53d6\u51fa\u4f86\uff0c\u5f9e\u800c\u5be6\u73fe\u4ee3\u78bc\u590d\u7528\uff0c\u63d0\u9ad8\u958b\u767c\u6548\u7387\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728React.js\u4e2d\u4f7f\u7528\u9ad8\u9636\u7d44\u4ef6\u9032\u884c\u4ee3\u78bc\u590d\u7528\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[184,183],"tags":[182,181],"class_list":["post-5929","post","type-post","status-publish","format-standard","hentry","category-react","category-react-js","tag-react","tag-react-js"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1xD","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5929"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5929\/revisions"}],"predecessor-version":[{"id":5930,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5929\/revisions\/5930"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}