{"id":5901,"date":"2023-01-06T15:31:19","date_gmt":"2023-01-06T07:31:19","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5901"},"modified":"2023-01-06T15:31:19","modified_gmt":"2023-01-06T07:31:19","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react-js%e4%b8%ad%e7%9a%84context%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%82%b3%e9%81%9e","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8react-js%e4%b8%ad%e7%9a%84context%e9%80%b2%e8%a1%8c%e6%95%b8%e6%93%9a%e5%82%b3%e9%81%9e\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528React.js\u4e2d\u7684Context\u9032\u884c\u6578\u64da\u50b3\u905e"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, Context, \u6570\u636e\u4f20\u9012\"><\/p>\n<h1>\u5728 React \u4e2d\u5982\u4f55\u4f7f\u7528 context \u8fdb\u884c\u6570\u636e\u4f20\u9012\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 \u4e2d\u7684 context \u662f\u4e00\u7a2e\u7528\u65bc\u5728\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u6578\u64da\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u5728\u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u6578\u64da\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528 context \u4f86\u9032\u884c\u6578\u64da\u50b3\u905e\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Context\uff1f<\/h2>\n<p>Context \u662f React \u4e2d\u7684\u4e00\u7a2e\u7279\u6b8a\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u6578\u64da\uff0c\u800c\u4e0d\u9700\u8981\u5728\u6bcf\u500b\u7d44\u4ef6\u4e2d\u90fd\u50b3\u905e\u6578\u64da\u3002Context \u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u5728\u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u6578\u64da\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u66f4\u65b0\u6578\u64da\uff0c\u800c\u4e0d\u9700\u8981\u5728\u6bcf\u500b\u7d44\u4ef6\u4e2d\u90fd\u66f4\u65b0\u6578\u64da\u3002<\/p>\n<h2>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528 Context\uff1f<\/h2>\n<p>\u5728 React \u4e2d\u4f7f\u7528 Context \u975e\u5e38\u7c21\u55ae\uff0c\u53ea\u9700\u8981\u5728\u7d44\u4ef6\u4e2d\u5275\u5efa\u4e00\u500b context \u5c0d\u8c61\uff0c\u7136\u5f8c\u5c07\u6578\u64da\u50b3\u905e\u7d66\u8a72 context \u5c0d\u8c61\uff0c\u5c31\u53ef\u4ee5\u5728\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u6578\u64da\u4e86\u3002<\/p>\n<pre class=\"brush: javascript\">\n\/\/ \u5275\u5efa\u4e00\u500b context \u5c0d\u8c61\nconst MyContext = React.createContext();\n\n\/\/ \u5c07\u6578\u64da\u50b3\u905e\u7d66 context \u5c0d\u8c61\n<MyContext.Provider value={data}>\n  {\/* \u7d44\u4ef6 *\/}\n<\/MyContext.Provider>\n<\/pre>\n<p>\u5728\u7d44\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>MyContext.Consumer<\/code> \u4f86\u8a2a\u554f context \u5c0d\u8c61\u4e2d\u7684\u6578\u64da\uff1a<\/p>\n<pre class=\"brush: javascript\">\n<MyContext.Consumer>\n  {value => {\n    \/\/ \u4f7f\u7528 context \u5c0d\u8c61\u4e2d\u7684\u6578\u64da\n  }}\n<\/MyContext.Consumer>\n<\/pre>\n<h2>\u7e3d\u7d50<\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u8a73\u7d30\u4ecb\u7d39\u4e86\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528 context \u4f86\u9032\u884c\u6578\u64da\u50b3\u905e\u3002Context \u662f React \u4e2d\u7684\u4e00\u7a2e\u7279\u6b8a\u529f\u80fd\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u6578\u64da\uff0c\u800c\u4e0d\u9700\u8981\u5728\u6bcf\u500b\u7d44\u4ef6\u4e2d\u90fd\u50b3\u905e\u6578\u64da\u3002\u4f7f\u7528 context \u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u5728\u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u6578\u64da\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u66f4\u65b0\u6578\u64da\uff0c\u800c\u4e0d\u9700\u8981\u5728\u6bcf\u500b\u7d44\u4ef6\u4e2d\u90fd\u66f4\u65b0\u6578\u64da\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5982\u679c\u4f60\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u95dc\u65bc React \u4e2d context \u7684\u77e5\u8b58\uff0c\u53ef\u4ee5\u53c3\u8003 <a href=\"https:\/\/reactjs.org\/docs\/context.html\" target=\"_blank\" rel=\"noopener\">React \u5b98\u65b9\u6587\u6a94<\/a> \u4e2d\u7684\u76f8\u95dc\u5167\u5bb9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aReact.js\u4e2d\u7684Context\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u6578\u64da\uff0c\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528Context\u9032\u884c\u6578\u64da\u50b3\u905e\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-5901","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-1xb","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5901","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=5901"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5901\/revisions"}],"predecessor-version":[{"id":5902,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5901\/revisions\/5902"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5901"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5901"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}