{"id":5863,"date":"2023-01-06T15:19:55","date_gmt":"2023-01-06T07:19:55","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5863"},"modified":"2023-01-06T15:19:55","modified_gmt":"2023-01-06T07:19:55","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8contextapi%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8contextapi%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528ContextAPI\uff1f"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, Context API\"><\/p>\n<h1>\u5982\u4f55\u5728 React \u4e2d\u4f7f\u7528 Context API\uff1f<\/h1>\n<p>React \u7684 Context API \u662f\u4e00\u500b\u5f88\u597d\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8b93\u4f60\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u5171\u4eab\u8cc7\u6599\uff0c\u800c\u4e0d\u9700\u8981\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u6bcf\u500b\u7d44\u4ef6\u3002\u5b83\u53ef\u4ee5\u8b93\u4f60\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u7684\u4efb\u4f55\u5730\u65b9\u5b58\u53d6\u8cc7\u6599\uff0c\u800c\u4e0d\u9700\u8981\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u6bcf\u500b\u7d44\u4ef6\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4f7f\u7528 Context API \u4e4b\u524d\uff0c\u4f60\u9700\u8981\u5148\u5efa\u7acb\u4e00\u500b Context \u7269\u4ef6\uff0c\u5b83\u53ef\u4ee5\u662f\u4e00\u500b JavaScript \u7269\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u500b\u51fd\u5f0f\uff0c\u7528\u65bc\u5efa\u7acb\u4e00\u500b\u65b0\u7684 Context \u7269\u4ef6\u3002<\/p>\n<p>\u63a5\u8457\uff0c\u4f60\u9700\u8981\u5728\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u4e2d\u4f7f\u7528 <code>React.createContext()<\/code> \u4f86\u5efa\u7acb\u4e00\u500b\u65b0\u7684 Context \u7269\u4ef6\uff0c\u4e26\u5c07\u5b83\u5132\u5b58\u5728\u4e00\u500b\u8b8a\u6578\u4e2d\uff1a<\/p>\n<pre class=\"brush: javascript\">\nconst MyContext = React.createContext();\n<\/pre>\n<p>\u63a5\u8457\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>MyContext.Provider<\/code> \u4f86\u63d0\u4f9b\u8cc7\u6599\u7d66\u4f60\u7684\u61c9\u7528\u7a0b\u5f0f\u4e2d\u7684\u7d44\u4ef6\uff1a<\/p>\n<pre class=\"brush: javascript\">\n<MyContext.Provider value={\/* some value *\/}>\n  <App \/>\n<\/MyContext.Provider>\n<\/pre>\n<p>\u6700\u5f8c\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>MyContext.Consumer<\/code> \u4f86\u5b58\u53d6\u8cc7\u6599\uff1a<\/p>\n<pre class=\"brush: javascript\">\n<MyContext.Consumer>\n  {value => \/* render something based on the context value *\/}\n<\/MyContext.Consumer>\n<\/pre>\n<p>\u4f7f\u7528 Context API \u53ef\u4ee5\u8b93\u4f60\u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u5171\u4eab\u8cc7\u6599\uff0c\u800c\u4e0d\u9700\u8981\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u6bcf\u500b\u7d44\u4ef6\uff0c\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u7a0b\u5f0f\u78bc\u7684\u91cd\u8907\uff0c\u4e26\u63d0\u9ad8\u61c9\u7528\u7a0b\u5f0f\u7684\u6548\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528ContextAPI\uff0c\u5f9e\u5b9a\u7fa9Context\u3001\u5efa\u7acbContext\u3001\u4f7f\u7528ContextProvider\u53caConsumer\u7b49\u6b65\u9a5f\uff0c\u8b93\u4f60\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u4f7f\u7528ContextAPI\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-5863","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-1wz","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5863","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=5863"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5863\/revisions"}],"predecessor-version":[{"id":5864,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5863\/revisions\/5864"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}