{"id":300,"date":"2021-09-23T19:43:56","date_gmt":"2021-09-23T11:43:56","guid":{"rendered":"http:\/\/gjweb.top\/?p=300"},"modified":"2021-09-23T19:43:56","modified_gmt":"2021-09-23T11:43:56","slug":"context","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=300","title":{"rendered":"context"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4f7f\u7528\u6b65\u9aa4<\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>\u8c03\u7528React.createContext() \u521b\u5efa Provider (\u63d0\u4f9b\u6570\u636e) \u548c Consumer (\u6d88\u8d39\u6570\u636e) \u4e24\u4e2a\u7ec4\u4ef6<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react'<br>const { Provider, Consumer} = React.createContext()<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>\u4f7f\u7528 <strong>Provider<\/strong> \u7ec4\u4ef6\u4f5c\u4e3a\u7236\u8282\u70b9<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Provider&gt;<br>    &lt;div&gt;<br> &nbsp;      &lt;Child1 \/&gt;<br> &nbsp; &nbsp;&lt;\/div&gt;<br>&lt;\/Provider&gt;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>\u8bbe\u7f6e <strong>value<\/strong> \u5c5e\u6027, \u8868\u793a\u8981\u4f20\u9012\u7684\u6570\u636e<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;provider value=\"xxx\"&gt;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\"><li>\u8c03\u7528 Consumer \u7ec4\u4ef6\u63a5\u6536\u6570\u636e<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Consumer&gt;<br> &nbsp;  {(data)=&gt;{<br> &nbsp; &nbsp; &nbsp; &nbsp;return (<br> &nbsp; &nbsp; &nbsp;      &lt;span&gt;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;      data \u53c2\u6570\u8868\u793a\u63a5\u6536\u7684\u6570\u636e---{data}<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;\/span&gt;<br> &nbsp; &nbsp; &nbsp;  )<br> &nbsp;  }}<br>&lt;\/Consumer&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u6b65\u9aa4 \u8c03\u7528React.createContext() \u521b\u5efa Provider (\u63d0\u4f9b\u6570\u636e) \u548c Consu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-300","post","type-post","status-publish","format-standard","hentry","category-react"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=300"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/300\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}