{"id":5653,"date":"2022-03-24T10:16:49","date_gmt":"2022-03-24T02:16:49","guid":{"rendered":"http:\/\/39.106.39.94\/?p=5653"},"modified":"2022-05-06T17:34:35","modified_gmt":"2022-05-06T09:34:35","slug":"context","status":"publish","type":"post","link":"https:\/\/lilichao.com\/?p=5653","title":{"rendered":"Context"},"content":{"rendered":"\n<p>\u5728React\u4e2d\u7ec4\u4ef6\u95f4\u7684\u6570\u636e\u901a\u4fe1\u662f\u901a\u8fc7props\u8fdb\u884c\u7684\uff0c\u7236\u7ec4\u4ef6\u7ed9\u5b50\u7ec4\u4ef6\u8bbe\u7f6eprops\uff0c\u5b50\u7ec4\u4ef6\u7ed9\u540e\u4ee3\u7ec4\u4ef6\u8bbe\u7f6eprops\uff0cprops\u5728\u7ec4\u4ef6\u95f4\u81ea\u4e0a\u5411\u4e0b\uff08\u7236\u4f20\u5b50\uff09\u7684\u9010\u5c42\u4f20\u9012\u6570\u636e\u3002\u4f46\u5e76\u4e0d\u662f\u6240\u6709\u7684\u6570\u636e\u90fd\u9002\u5408\u8fd9\u79cd\u4f20\u9012\u65b9\u5f0f\uff0c\u6709\u4e9b\u6570\u636e\u9700\u8981\u5728\u591a\u4e2a\u7ec4\u4ef6\u4e2d\u5171\u540c\u4f7f\u7528\uff0c\u5982\u679c\u8fd8\u901a\u8fc7props\u4e00\u5c42\u4e00\u5c42\u4f20\u9012\uff0c\u9ebb\u70e6\u81ea\u4e0d\u5fc5\u591a\u8bf4\u3002<\/p>\n\n\n\n<p>Context\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u79cd\u5728\u4e0d\u540c\u7ec4\u4ef6\u95f4\u5171\u4eab\u6570\u636e\u7684\u65b9\u5f0f\uff0c\u5b83\u4e0d\u518d\u62d8\u6ce5\u4e8eprops\u523b\u677f\u7684\u9010\u5c42\u4f20\u9012\uff0c\u800c\u662f\u5728\u5916\u5c42\u7ec4\u4ef6\u4e2d\u7edf\u4e00\u8bbe\u7f6e\uff0c\u8bbe\u7f6e\u540e\u5185\u5c42\u6240\u6709\u7684\u7ec4\u4ef6\u90fd\u53ef\u4ee5\u8bbf\u95ee\u5230Context\u4e2d\u6240\u5b58\u50a8\u7684\u6570\u636e\u3002\u6362\u53e5\u8bdd\u8bf4\uff0cContext\u7c7b\u4f3c\u4e8eJS\u4e2d\u7684\u5168\u5c40\u4f5c\u7528\u57df\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e9b\u516c\u5171\u6570\u636e\u8bbe\u7f6e\u5230\u4e00\u4e2a\u540c\u4e00\u4e2aContext\u4e2d\uff0c\u4f7f\u5f97\u6240\u6709\u7684\u7ec4\u4ef6\u90fd\u53ef\u4ee5\u8bbf\u95ee\u5230\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u521b\u5efaContext\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const MyContext = React.createContext(defaultValue);\n<\/code><\/pre>\n\n\n\n<p><code>React.createContext(defaultValue)<\/code>\u7528\u6765\u521b\u5efa\u4e00\u4e2aContext\u5bf9\u8c61\uff0c\u5b83\u9700\u8981\u4e00\u4e2a\u521d\u59cb\u503c\u4f5c\u4e3a\u53c2\u6570\uff0c\u8fd9\u4e2a\u521d\u59cb\u503c\u53ef\u4ee5\u662f\u4e00\u4e2a\u539f\u59cb\u503c\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2aJS\u5bf9\u8c61\u3002\u8c03\u7528\u4ee5\u540e\uff0c\u65b9\u6cd5\u5c06\u4f1a\u8fd4\u56de\u4e00\u4e2aContext\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u5bf9\u8c61\u975e\u5e38\u5173\u952e\uff0c\u5f53\u6211\u4eec\u60f3\u5728\u5176\u4ed6\u7ec4\u4ef6\u4e2d\u8bbf\u95eeContext\u4e2d\u7684\u6570\u636e\u65f6\uff0c\u5fc5\u987b\u8981\u901a\u8fc7\u8fd9\u4e2a\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u7531\u4e8eContext\u5bf9\u8c61\u9700\u8981\u5728\u4e0d\u540c\u7684\u7ec4\u4ef6\u4e2d\u88ab\u4f7f\u7528\uff0c\u6240\u4ee5\u901a\u5e38\u6211\u4eec\u4f1a\u5c06Context\u5bf9\u8c61\u8bbe\u7f6e\u5230\u4e00\u4e2a\u5355\u72ec\u7684\u6a21\u5757\u4e2d\u5e76\u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u5bfc\u51fa\u50cf\u662f\u8fd9\u6837\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\";\n\nconst TestContext = React.createContext({\n    name:'\u5b59\u609f\u7a7a',\n    age:18,\n    gender:'\u7537',\n    sayHello:()=&gt;{\n        alert(this.name);    \n    }\n});\n\nexport default TestContext;\n<\/code><\/pre>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\u6211\u4eec\u66b4\u9732\u7684\u6570\u636e\u6bd4\u8f83\u7b80\u5355\uff0c\u5c31\u662f\u4e00\u4e2a\u7b80\u5355\u7684JS\u5bf9\u8c61\uff0c\u5176\u4e2d\u5305\u542b\u4e86\u4e09\u4e2a\u5c5e\u6027\u548c\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5e76\u6700\u4e2d\u5c06\u4ea7\u751f\u7684Context\u5bf9\u8c61\u4f5c\u4e3a\u9ed8\u8ba4\u6a21\u5757\u5411\u5916\u90e8\u5bfc\u51fa\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u60f3\u8bbf\u95ee\u5230Context\u4e2d\u7684\u6570\u636e\uff0c\u6211\u4eec\u9700\u8981\u5148\u5c06Context\u5f15\u5165\u5230\u5f53\u524d\u7ec4\u4ef6\u4e2d\uff0c\u7136\u540e\u901a\u8fc7Context\u5bf9\u8c61\u8bbf\u95ee\u5176\u4e2d\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e00\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u901a\u8fc7Consumer\u6807\u7b7e\u6765\u8bbf\u95ee\u5230Context\u4e2d\u7684\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import <strong><em>React <\/em><\/strong>from 'react';\nimport <strong><em>TestContext <\/em><\/strong>from '..\/store\/test-context';\n\nconst MyComponent = () =&gt; {\n\n    return (\n        &lt;TestContext.Consumer&gt;\n            {(ctx)=&gt;{\n                return (\n                    &lt;ul&gt;\n                        &lt;li&gt;{ctx.name}&lt;\/li&gt;\n                        &lt;li&gt;{ctx.age}&lt;\/li&gt;\n                        &lt;li&gt;{ctx.gender}&lt;\/li&gt;\n                    &lt;\/ul&gt;\n                );\n            }}\n        &lt;\/TestContext.Consumer&gt;\n\n    );\n};\n\nexport default MyComponent;<\/pre>\n\n\n\n<p>\u8bbf\u95eeContext\u9996\u5148\u6211\u4eec\u9700\u8981\u5f15\u5165\u4e4b\u524d\u521b\u5efa\u7684Context\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import TestContext from '..\/store\/test-context';<\/code><\/pre>\n\n\n\n<p>Context\u5bf9\u8c61\u4e2d\u6709\u4e00\u4e2a\u5c5e\u6027\u53eb\u505aConsumer\uff0c\u76f4\u8bd1\u8fc7\u6765\u4e3a\u6d88\u8d39\u8005\uff0c\u5982\u679c\u4f60\u4e86\u89e3\u751f\u4ea7\u6d88\u8d39\u8005\u6a21\u5f0f\u8fd9\u91cc\u5c31\u6bd4\u8f83\u597d\u7406\u89e3\u4e86\uff0c\u5982\u679c\u6ca1\u63a5\u89e6\u8fc7\uff0c\u4f60\u53ef\u4ee5\u5c06Consumer\u7406\u89e3\u4e3a\u6570\u636e\u7684\u83b7\u53d6\u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u5c06\u5b83\u7406\u89e3\u4e3a\u4e00\u4e2a\u7279\u6b8a\u7684\u7ec4\u4ef6\uff0c\u6240\u4ee5\u4f60\u9700\u8981\u8fd9\u6837\u4f7f\u7528\u5b83\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;TestContext.Consumer&gt;<br>    {(ctx)=&gt;{<br>        return (<br>            &lt;ul&gt;<br>                &lt;li&gt;{ctx.name}&lt;\/li&gt;<br>                &lt;li&gt;{ctx.age}&lt;\/li&gt;<br>                &lt;li&gt;{ctx.gender}&lt;\/li&gt;<br>            &lt;\/ul&gt;<br>        );<br>    }}<br>&lt;\/TestContext.Consumer&gt;<\/pre>\n\n\n\n<p>Consumer\u7684\u6807\u7b7e\u4f53\u5fc5\u987b\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u4f1a\u5728\u7ec4\u4ef6\u6e32\u67d3\u65f6\u8c03\u7528\u5e76\u4e14\u5c06Context\u4e2d\u5b58\u50a8\u7684\u6570\u636e\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u8fdb\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5c06\u4f1a\u4f5c\u4e3a\u7ec4\u4ef6\u88ab\u6700\u7ec8\u6e32\u67d3\u5230\u9875\u9762\u4e2d\u3002\u8fd9\u91cc\u6211\u4eec\u5c06\u53c2\u6570\u547d\u540d\u4e3a\u4e86ctx\uff0c\u5728\u56de\u8c03\u51fd\u6570\u4e2d\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7ctx.xxx\u8bbf\u95ee\u5230Context\u4e2d\u7684\u6570\u636e\u3002\u5982\u679c\u9700\u8981\u8bbf\u95ee\u591a\u4e2aContext\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2aConsumer\u5d4c\u5957\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7Consumer\u4f7f\u7528Context\u5b9e\u5728\u662f\u4e0d\u591f\u4f18\u96c5\uff0c\u6240\u4ee5React\u8fd8\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4e00\u4e2a\u94a9\u5b50\u51fd\u6570`useContext()`\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5c06Context\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u94a9\u5b50\u51fd\u6570\uff0c\u5b83\u5c31\u4f1a\u76f4\u63a5\u7ed9\u6211\u4eec\u8fd4\u56deContext\u5bf9\u8c61\u4e2d\u5b58\u50a8\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React, {useContext} from 'react';\nimport TestContext from '..\/store\/test-context';\n\nconst MyComponent = () =&gt; {\n\n    const ctx = useContext(TestContext);\n\n    return (\n        &lt;ul&gt;\n            &lt;li&gt;{ctx.name}&lt;\/li&gt;\n            &lt;li&gt;{ctx.age}&lt;\/li&gt;\n            &lt;li&gt;{ctx.gender}&lt;\/li&gt;\n        &lt;\/ul&gt;\n    );\n};\n\nexport default MyComponent;\n<\/code><\/pre>\n\n\n\n<p>\u50cf\u4e0a\u8fb9\u90a3\u6837\u4f7f\u7528Context\u5e76\u4e0d\u5341\u5206\u5e38\u89c1\uff0c\u56e0\u4e3a\u8fd9\u79cd\u65b9\u5f0f\u4e2dContext\u7684\u503c\u662f\u5199\u6b7b\u7684\uff0c\u5e76\u4e0d\u662f\u5728\u7ec4\u4ef6\u4e2d\u6307\u5b9a\u7684\u3002\u6240\u4ee5React\u8fd8\u63d0\u4f9b\u4e86Provider\uff0c\u7528\u6765\u5728\u7ec4\u4ef6\u4e2d\u6307\u5b9aContext\u503c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\";\nimport MyComponent from \".\/component\/MyComponent\";\nimport TestContext from \".\/store\/test-context\";\n\nconst App = () =&gt; {\n\n\n    return &lt;TestContext.Provider value={{name:'\u732a\u516b\u6212', age:28, gender:'\u7537'}}&gt;\n        &lt;MyComponent\/&gt;\n    &lt;\/TestContext.Provider&gt;;\n};\n\nexport default App;\n<\/code><\/pre>\n\n\n\n<p>Provider\u8bd1\u4e3a\u751f\u4ea7\u8005\uff0c\u548cConsumer\u6d88\u8d39\u8005\u5bf9\u5e94\u3002Provider\u4f1a\u8bbe\u7f6e\u5728\u5916\u5c42\u7ec4\u4ef6\u4e2d\uff0c\u901a\u8fc7value\u5c5e\u6027\u6765\u6307\u5b9aContext\u7684\u503c\u3002\u8fd9\u4e2aContext\u503c\u5728\u6240\u6709\u7684Provider\u5b50\u7ec4\u4ef6\u4e2d\u90fd\u53ef\u4ee5\u8bbf\u95ee\u3002Context\u7684\u641c\u7d22\u6d41\u7a0b\u548cJS\u4e2d\u51fd\u6570\u4f5c\u7528\u57df\u7c7b\u4f3c\uff0c\u5f53\u6211\u4eec\u83b7\u53d6Context\u65f6\uff0cReact\u4f1a\u5728\u5b83\u7684\u5916\u5c42\u67e5\u627e\u6700\u8fd1\u7684Provider\uff0c\u7136\u540e\u8fd4\u56de\u5b83\u7684Context\u503c\u3002\u5982\u679c\u6ca1\u6709\u627e\u5230Provider\uff0c\u5219\u4f1a\u8fd4\u56deContext\u6a21\u5757\u4e2d\u8bbe\u7f6e\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728React\u4e2d\u7ec4\u4ef6\u95f4\u7684\u6570\u636e\u901a\u4fe1\u662f\u901a\u8fc7props\u8fdb\u884c\u7684\uff0c\u7236\u7ec4\u4ef6\u7ed9\u5b50\u7ec4\u4ef6\u8bbe\u7f6eprops\uff0c\u5b50\u7ec4\u4ef6\u7ed9\u540e\u4ee3\u7ec4\u4ef6\u8bbe\u7f6epro [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5657,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[198],"tags":[200,202,199,201,203],"_links":{"self":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5653"}],"collection":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5653"}],"version-history":[{"count":3,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5653\/revisions"}],"predecessor-version":[{"id":5771,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/posts\/5653\/revisions\/5771"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=\/wp\/v2\/media\/5657"}],"wp:attachment":[{"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lilichao.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}