{"id":4548,"date":"2023-01-04T16:07:16","date_gmt":"2023-01-04T08:07:16","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=4548"},"modified":"2023-01-04T16:07:16","modified_gmt":"2023-01-04T08:07:16","slug":"%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8react%e5%85%84%e5%bc%9f%e7%b5%84%e4%bb%b6%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/react-js\/%e5%ad%b8%e7%bf%92%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8react%e5%85%84%e5%bc%9f%e7%b5%84%e4%bb%b6%e9%80%9a%e4%bf%a1\/","title":{"rendered":"\u5b78\u7fd2\u5982\u4f55\u5229\u7528React\u5144\u5f1f\u7d44\u4ef6\u901a\u4fe1"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"React, React \u5144\u5f1f\u7d44\u4ef6\u901a\u4fe1, context\"><\/p>\n<h1>React \u5144\u5f1f\u7d44\u4ef6\u901a\u4fe1(context)<\/h1>\n<p>React \u662f\u4e00\u500b\u7531 Facebook \u6240\u958b\u767c\u7684 JavaScript \u51fd\u5f0f\u5eab\uff0c\u53ef\u4ee5\u7528\u4f86\u5efa\u7acb\u7528\u6236\u7aef\u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5efa\u7acb\u53ef\u91cd\u8907\u4f7f\u7528\u7684 UI \u7d44\u4ef6\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5efa\u7acb\u548c\u7dad\u8b77\u5927\u578b\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n<p>React \u5144\u5f1f\u7d44\u4ef6\u901a\u4fe1(context)\u662f\u4e00\u7a2e\u6280\u8853\uff0c\u53ef\u4ee5\u8b93 React \u7d44\u4ef6\u4e4b\u9593\u9032\u884c\u901a\u4fe1\uff0c\u800c\u4e0d\u9700\u8981\u900f\u904e\u7236\u7d44\u4ef6\u50b3\u905e props\u3002\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u61c9\u7528\u7a0b\u5f0f\u7684\u4e0d\u540c\u90e8\u5206\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u61c9\u7528\u7a0b\u5f0f\u7684\u8cc7\u6599\u6d41\u3002<\/p>\n<p><!--more--><\/p>\n<p>React \u5144\u5f1f\u7d44\u4ef6\u901a\u4fe1(context)\u7684\u57fa\u672c\u539f\u7406\u662f\uff0c\u5b83\u4f7f\u7528\u4e00\u500b\u7279\u6b8a\u7684 React \u7d44\u4ef6\uff0c\u7a31\u70ba <strong>Context Provider<\/strong>\uff0c\u4f86\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u61c9\u7528\u7a0b\u5f0f\u7684\u4e0d\u540c\u90e8\u5206\u3002Context Provider \u53ef\u4ee5\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u5b83\u7684\u5b50\u7d44\u4ef6\uff0c\u4e26\u4e14\u53ef\u4ee5\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u5b83\u7684\u5144\u5f1f\u7d44\u4ef6\u3002<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 React \u5144\u5f1f\u7d44\u4ef6\u901a\u4fe1(context)<\/h2>\n<p>\u8981\u4f7f\u7528 React \u5144\u5f1f\u7d44\u4ef6\u901a\u4fe1(context)\uff0c\u9996\u5148\u9700\u8981\u5efa\u7acb\u4e00\u500b Context Provider \u7d44\u4ef6\uff0c\u4e26\u4e14\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u5b83\u7684\u5b50\u7d44\u4ef6\uff1a<\/p>\n<pre class=\"brush: javascript\">\nimport React, { createContext } from 'react';\n\nconst MyContext = createContext();\n\nclass MyProvider extends React.Component {\n  state = {\n    name: 'John',\n    age: 30\n  };\n\n  render() {\n    return (\n      <MyContext.Provider\n        value={{\n          state: this.state\n        }}\n      >\n        {this.props.children}\n      <\/MyContext.Provider>\n    );\n  }\n}\n\nexport default MyProvider;\n<\/pre>\n<p>\u63a5\u8457\uff0c\u53ef\u4ee5\u5728\u5176\u4ed6\u7d44\u4ef6\u4e2d\u4f7f\u7528 Context Provider\uff0c\u4ee5\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u5b83\u7684\u5b50\u7d44\u4ef6\uff1a<\/p>\n<pre class=\"brush: javascript\">\nimport React, { Component } from 'react';\nimport MyContext from '.\/MyContext';\n\nclass MyComponent extends Component {\n  render() {\n    return (\n      <MyContext.Consumer>\n        {(context) => (\n          <React.Fragment>\n            Name: {context.state.name}\n            Age: {context.state.age}\n          <\/React.Fragment>\n        )}\n      <\/MyContext.Consumer>\n    );\n  }\n}\n\nexport default MyComponent;\n<\/pre>\n<p>\u6700\u5f8c\uff0c\u53ef\u4ee5\u5728\u5176\u4ed6\u7d44\u4ef6\u4e2d\u4f7f\u7528 Context Provider\uff0c\u4ee5\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u5b83\u7684\u5144\u5f1f\u7d44\u4ef6\uff1a<\/p>\n<pre class=\"brush: javascript\">\nimport React, { Component } from 'react';\nimport MyContext from '.\/MyContext';\n\nclass MySiblingComponent extends Component {\n  render() {\n    return (\n      <MyContext.Consumer>\n        {(context) => (\n          <React.Fragment>\n            Name: {context.state.name}\n            Age: {context.state.age}\n          <\/React.Fragment>\n        )}\n      <\/MyContext.Consumer>\n    );\n  }\n}\n\nexport default MySiblingComponent;\n<\/pre>\n<p>React \u5144\u5f1f\u7d44\u4ef6\u901a\u4fe1(context)\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u5c07\u8cc7\u6599\u50b3\u905e\u5230\u61c9\u7528\u7a0b\u5f0f\u7684\u4e0d\u540c\u90e8\u5206\uff0c\u4e26\u4e14\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u61c9\u7528\u7a0b\u5f0f\u7684\u8cc7\u6599\u6d41\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1aReact\u5144\u5f1f\u7d44\u4ef6\u901a\u4fe1\u662f\u4e00\u7a2e\u8b93\u5169\u500b\u7368\u7acb\u7684React\u7d44\u4ef6\u4e4b\u9593\u9032\u884c\u6e9d\u901a\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u5728\u4e0d\u540c\u7684\u7d44\u4ef6\u4e4b\u9593\u50b3\u905e\u8cc7\u6599\uff0c\u66f4\u6709\u6548\u5730\u5efa\u7acb\u61c9\u7528\u7a0b\u5f0f\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u5229\u7528React\u5144\u5f1f\u7d44\u4ef6\u901a\u4fe1\u4f86\u5efa\u7acb\u61c9\u7528\u7a0b\u5f0f\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-4548","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-1bm","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4548","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=4548"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4548\/revisions"}],"predecessor-version":[{"id":4549,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/4548\/revisions\/4549"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=4548"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=4548"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=4548"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}