{"id":5935,"date":"2023-01-06T15:40:26","date_gmt":"2023-01-06T07:40:26","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5935"},"modified":"2023-01-06T15:40:26","modified_gmt":"2023-01-06T07:40:26","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8reactcontext%e9%80%b2%e8%a1%8c%e7%8a%b6%e6%80%81%e7%ae%a1%e7%90%86%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8reactcontext%e9%80%b2%e8%a1%8c%e7%8a%b6%e6%80%81%e7%ae%a1%e7%90%86%ef%bc%9f\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528ReactContext\u9032\u884c\u72b6\u6001\u7ba1\u7406\uff1f"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, Context, \u72b6\u6001\u7ba1\u7406\"><\/p>\n<h1>\u5728 React \u4e2d\u5982\u4f55\u4f7f\u7528 Context \u8fdb\u884c\u72b6\u6001\u7ba1\u7406\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\u500b\u53eb\u505a Context \u7684 API\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u7ba1\u7406\u7d44\u4ef6\u4e4b\u9593\u7684\u72c0\u614b\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 React \u7684 Context API \u4f86\u7ba1\u7406\u7d44\u4ef6\u4e4b\u9593\u7684\u72c0\u614b\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662f Context\uff1f<\/h2>\n<p>Context \u662f React \u4e2d\u7684\u4e00\u500b API\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u5c64\u7d1a\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u72c0\u614b\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u5c64\u7d1a\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u72c0\u614b\uff0c\u800c\u4e0d\u9700\u8981\u901a\u904e props \u5c07\u8cc7\u6599\u50b3\u905e\u5230\u6bcf\u500b\u7d44\u4ef6\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 Context\uff1f<\/h2>\n<p>\u4f7f\u7528 Context \u7684\u7b2c\u4e00\u6b65\u662f\u5275\u5efa\u4e00\u500b Context \u5c0d\u8c61\uff0c\u5b83\u53ef\u4ee5\u662f\u4e00\u500b JavaScript \u5c0d\u8c61\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u500b\u51fd\u6578\u3002\u63a5\u4e0b\u4f86\uff0c\u60a8\u9700\u8981\u5c07 Context \u5c0d\u8c61\u50b3\u905e\u7d66 <code>React.createContext()<\/code> \u51fd\u6578\uff0c\u4e26\u5c07\u5176\u50b3\u905e\u7d66\u60a8\u7684\u7d44\u4ef6\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>React.useContext()<\/code> \u51fd\u6578\u4f86\u8a2a\u554f Context \u5c0d\u8c61\u4e2d\u7684\u8cc7\u6599\u3002<\/p>\n<pre class=\"brush: javascript\">\n\/\/ \u5275\u5efa\u4e00\u500b Context \u5c0d\u8c61\nconst MyContext = React.createContext();\n\n\/\/ \u5c07 Context \u5c0d\u8c61\u50b3\u905e\u7d66\u7d44\u4ef6\nfunction MyComponent() {\n  const context = React.useContext(MyContext);\n  \/\/ \u4f7f\u7528 context \u4e2d\u7684\u8cc7\u6599\n  return (\n    <div>\n      {context.data}\n    <\/div>\n  );\n}\n<\/pre>\n<h2>\u7e3d\u7d50<\/h2>\n<p>React \u7684 Context API \u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u7ba1\u7406\u7d44\u4ef6\u4e4b\u9593\u7684\u72c0\u614b\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u5c64\u7d1a\u7684\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u72c0\u614b\uff0c\u800c\u4e0d\u9700\u8981\u901a\u904e props \u5c07\u8cc7\u6599\u50b3\u905e\u5230\u6bcf\u500b\u7d44\u4ef6\u3002\u4f7f\u7528 Context \u7684\u7b2c\u4e00\u6b65\u662f\u5275\u5efa\u4e00\u500b Context \u5c0d\u8c61\uff0c\u7136\u5f8c\u5c07\u5176\u50b3\u905e\u7d66\u60a8\u7684\u7d44\u4ef6\uff0c\u4e26\u4f7f\u7528 <code>React.useContext()<\/code> \u51fd\u6578\u4f86\u8a2a\u554f Context \u5c0d\u8c61\u4e2d\u7684\u8cc7\u6599\u3002<br \/>\n<!--more--><br \/>\nReact \u7684 Context API \u662f\u4e00\u500b\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u8f15\u9b06\u5730\u7ba1\u7406\u7d44\u4ef6\u4e4b\u9593\u7684\u72c0\u614b\u3002\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u4e00\u7a2e\u66f4\u8f15\u9b06\u7684\u65b9\u5f0f\u4f86\u7ba1\u7406\u7d44\u4ef6\u4e4b\u9593\u7684\u72c0\u614b\uff0c\u90a3\u9ebc React \u7684 Context API \u53ef\u80fd\u662f\u4e00\u500b\u5f88\u597d\u7684\u9078\u64c7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aReactContext\u662f\u4e00\u7a2e\u7528\u65bc\u7ba1\u7406\u7d44\u4ef6\u4e4b\u9593\u5171\u4eab\u6578\u64da\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u8b93\u60a8\u5728\u61c9\u7528\u7a0b\u5e8f\u7684\u4e0d\u540c\u90e8\u5206\u4e4b\u9593\u5171\u4eab\u72b6\u6001\uff0c\u800c\u7121\u9700\u660e\u78ba\u5730\u50b3\u905e\u6578\u64da\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528ReactContext\u9032\u884c\u72b6\u6001\u7ba1\u7406\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-5935","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-1xJ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5935","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=5935"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5935\/revisions"}],"predecessor-version":[{"id":5936,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5935\/revisions\/5936"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}