{"id":53420,"date":"2024-12-03T17:17:22","date_gmt":"2024-12-03T09:17:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/53420\/"},"modified":"2024-12-03T17:17:22","modified_gmt":"2024-12-03T09:17:22","slug":"usecontext%ef%bc%9areact-hooks-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/53420\/","title":{"rendered":"useContext\uff1aReact Hooks"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>useContext\uff1aReact Hooks<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span>    <\/p>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300auseContext\uff1aReact Hooks\u300b<\/span>\u4e3b\u8981\u8bb2\u89e3\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241130\/1732942487674a9a97aa43c.jpg\" class=\"aligncenter\" title=\"useContext\uff1aReact Hooks\u63d2\u56fe\" alt=\"useContext\uff1aReact Hooks\u63d2\u56fe\" \/><\/p>\n<h2> react \u4e2d\u7684 usecontext \u548c\u4e24\u4e2a\u5b9e\u7528\u7684\u8ff7\u4f60\u9879\u76ee <\/h2>\n<h2> \u4ecb\u7ecd <\/h2>\n<p>\u5728 react \u4e2d\uff0c\u7ba1\u7406\u7ec4\u4ef6\u4e4b\u95f4\u7684\u6570\u636e\u81f3\u5173\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u591a\u4e2a\u7ec4\u4ef6\u9700\u8981\u8bbf\u95ee\u76f8\u540c\u6570\u636e\u7684\u5927\u578b\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002 prop \u94bb\u53d6\uff08\u5c06 props \u4f20\u9012\u5230\u7ec4\u4ef6\u6811\u7684\u591a\u4e2a\u7ea7\u522b\uff09\u5f88\u5feb\u5c31\u4f1a\u53d8\u5f97\u5f88\u9ebb\u70e6\u3002\u8fd9\u5c31\u662f react \u7684 usecontext hook \u7684\u95ea\u5149\u70b9\u3002 usecontext \u5141\u8bb8\u60a8\u8de8\u7ec4\u4ef6\u5171\u4eab\u6570\u636e\uff0c\u800c\u65e0\u9700\u624b\u52a8\u4f20\u9012 props\uff0c\u8fd9\u4f7f\u5176\u6210\u4e3a\u72b6\u6001\u7ba1\u7406\u7684\u5b9d\u8d35\u5de5\u5177\u3002<\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u9996\u5148\u8be6\u7ec6\u89e3\u91ca usecontext\u3001\u5176\u8bed\u6cd5\u53ca\u5176\u4f18\u70b9\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u6784\u5efa\u4e24\u4e2a\u8ff7\u4f60\u9879\u76ee\u6765\u5de9\u56fa\u8fd9\u79cd\u7406\u89e3\uff1a<\/p>\n<ol>\n<li> <strong>\u4e3b\u9898\u5207\u6362\u5668<\/strong>\uff1a\u4e00\u4e2a\u7b80\u5355\u7684\u4e3b\u9898\u5207\u6362\u5668\uff0c\u7528\u4e8e\u5c55\u793a usecontext \u5982\u4f55\u7ba1\u7406\u5168\u5c40\u72b6\u6001\u3002<\/li>\n<li> <strong>\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001<\/strong>\uff1a\u5904\u7406\u7528\u6237\u767b\u5f55\u72b6\u6001\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u6f14\u793a\u5982\u4f55\u5728\u5b9e\u9645\u7528\u4f8b\u4e2d\u4f7f\u7528 usecontext\u3002<\/li>\n<\/ol>\n<p>\u5728\u672c\u6559\u7a0b\u7ed3\u675f\u65f6\uff0c\u60a8\u5c06\u5177\u5907\u5728\u4efb\u4f55 react \u9879\u76ee\u4e2d\u81ea\u4fe1\u5730\u4f7f\u7528 usecontext \u7684\u80fd\u529b\u3002<\/p>\n<hr>\n<h2> \u4ec0\u4e48\u662fusecontext\uff1f <\/h2>\n<h3> usecontext \u7684\u57fa\u7840\u77e5\u8bc6 <\/h3>\n<p>usecontext \u662f\u4e00\u4e2a react hook\uff0c\u5141\u8bb8\u7ec4\u4ef6\u76f4\u63a5\u8ba2\u9605\u4e0a\u4e0b\u6587\u3002\u5b83\u4f7f\u7ec4\u4ef6\u80fd\u591f\u4ece\u7ec4\u4ef6\u6811\u4e2d\u4f4d\u4e8e\u5176\u4e0a\u65b9\u7684\u6700\u8fd1\u7684\u63d0\u4f9b\u7a0b\u5e8f\u8bbf\u95ee\u5168\u5c40\u6570\u636e\uff0c\u4ece\u800c\u6709\u52a9\u4e8e\u907f\u514d prop \u94bb\u53d6\u7684\u9ebb\u70e6\u3002<\/p>\n<h3> usecontext \u7684\u8bed\u6cd5 <\/h3>\n<p>\u4ee5\u4e0b\u662f\u521b\u5efa\u548c\u4f7f\u7528\u4e0a\u4e0b\u6587\u7684\u57fa\u672c\u8bed\u6cd5\uff1a<\/p>\n<pre>import react, { usecontext, createcontext } from 'react';\n\nconst mycontext = createcontext(defaultvalue); \/\/ step 1: create a context\n\nfunction mycomponent() {\n    const contextvalue = usecontext(mycontext); \/\/ step 2: use the context\n    return &lt;div&gt;{contextvalue}&lt;\/div&gt;;\n}\n<\/pre>\n<h3> \u89e3\u91ca <\/h3>\n<ol>\n<li> <strong>\u521b\u5efa\u4e0a\u4e0b\u6587<\/strong>\uff1acreatecontext \u521d\u59cb\u5316\u4e00\u4e2a\u4e0a\u4e0b\u6587\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u4fdd\u5b58\u6211\u4eec\u8981\u5171\u4eab\u7684\u6570\u636e\u3002 defaultvalue \u53c2\u6570\u662f\u53ef\u9009\u7684\uff0c\u4f46\u5982\u679c\u6ca1\u6709\u627e\u5230 provider\uff0c\u53ef\u4ee5\u7528\u4f5c\u540e\u5907\u53c2\u6570\u3002<\/li>\n<li> <strong>\u901a\u8fc7 usecontext \u4f7f\u7528\u4e0a\u4e0b\u6587<\/strong>\uff1a\u5728\u7ec4\u4ef6\u5185\u90e8\uff0c\u6211\u4eec\u4f7f\u7528 usecontext(mycontext) \u6765\u8bbf\u95ee\u4e0a\u4e0b\u6587\u7684\u5f53\u524d\u503c\u3002\u8be5\u503c\u7531\u7ec4\u4ef6\u6811\u4e2d\u8be5\u7ec4\u4ef6\u4e0a\u65b9\u6700\u8fd1\u7684 &lt;mycontext.provider&gt; \u786e\u5b9a\u3002<\/li>\n<\/ol>\n<h3> \u4e0a\u4e0b\u6587\u63d0\u4f9b\u8005\u548c\u6d88\u8d39\u8005\u7684\u793a\u4f8b <\/h3>\n<pre>import react, { usecontext, createcontext } from 'react';\n\nconst themecontext = createcontext('light'); \/\/ default theme is light\n\nfunction themeprovider({ children }) {\n    return &lt;themecontext.provider value=\"dark\"&gt;{children}&lt;\/themecontext.provider&gt;;\n}\n\nfunction displaytheme() {\n    const theme = usecontext(themecontext); \/\/ consuming the context\n    return &lt;p&gt;the current theme is {theme}&lt;\/p&gt;;\n}\n\nfunction app() {\n    return (\n        &lt;themeprovider&gt;\n            &lt;displaytheme \/&gt;\n        &lt;\/themeprovider&gt;\n    );\n}\n<\/pre>\n<p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff1a<\/p>\n<ul>\n<li> <strong>themecontext<\/strong> \u662f\u6211\u4eec\u7684\u4e0a\u4e0b\u6587\uff0c\u4f7f\u7528\u9ed8\u8ba4\u503c\u201clight\u201d\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/li>\n<li> <strong>themeprovider<\/strong> \u5305\u88c5 displaytheme \u5e76\u63d0\u4f9b value=&#8221;dark&#8221;\uff0c\u4f7f\u201cdark\u201d\u6210\u4e3a themeprovider \u4e2d\u7684\u5f53\u524d\u4e3b\u9898\u3002<\/li>\n<li> <strong>displaytheme<\/strong> \u7ec4\u4ef6\u4f7f\u7528 usecontext(themecontext) \u6765\u8bbf\u95ee\u4e3b\u9898\u5e76\u6e32\u67d3\u5b83\u3002<\/li>\n<\/ul>\n<p>\u8fd9\u6db5\u76d6\u4e86\u57fa\u7840\u77e5\u8bc6\u3002\u73b0\u5728\uff0c\u8ba9\u6211\u4eec\u6df1\u5165\u7814\u7a76\u9879\u76ee\uff0c\u5c06\u8fd9\u4e9b\u77e5\u8bc6\u5e94\u7528\u5230\u5b9e\u9645\u573a\u666f\u4e2d\u3002<\/p>\n<hr>\n<h2> \u8ff7\u4f60\u9879\u76ee 1\uff1a\u6784\u5efa\u4e3b\u9898\u5207\u6362\u5668 <\/h2>\n<p>\u6211\u4eec\u7684\u7b2c\u4e00\u4e2a\u9879\u76ee\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u4e3b\u9898\u5207\u6362\u5668\uff0c\u5b83\u5c06\u6f14\u793a\u5982\u4f55\u4f7f\u7528 usecontext \u6765\u7ba1\u7406\u4e3b\u9898\u7684\u5168\u5c40\u5e94\u7528\u7a0b\u5e8f\u72b6\u6001\u3002<\/p>\n<h3> \u7b2c 1 \u6b65\uff1a\u8bbe\u7f6e\u4e0a\u4e0b\u6587 <\/h3>\n<pre>import react, { createcontext, usecontext, usestate } from 'react';\n\nconst themecontext = createcontext();\n\nexport function themeprovider({ children }) {\n    const [theme, settheme] = usestate('light');\n    const toggletheme = () =&gt; settheme(theme === 'light' ? 'dark' : 'light');\n\n    return (\n        &lt;themecontext.provider value={{ theme, toggletheme }}&gt;\n            {children}\n        &lt;\/themecontext.provider&gt;\n    );\n}\n<\/pre>\n<p>\u8fd9\u91cc\uff0cthemecontext \u63d0\u4f9b\u4e24\u4e2a\u503c\uff1a\u5f53\u524d\u4e3b\u9898\u548c\u5207\u6362\u5b83\u7684\u51fd\u6570\u3002\u63d0\u4f9b\u7a0b\u5e8f\u5305\u88c5\u5e94\u7528\u7a0b\u5e8f\u7ec4\u4ef6\uff0c\u4f7f\u4e3b\u9898\u548c\u5207\u6362\u529f\u80fd\u5168\u5c40\u53ef\u7528\u3002<\/p>\n<h3> \u7b2c 2 \u6b65\uff1a\u4f7f\u7528\u7ec4\u4ef6\u4e2d\u7684\u4e0a\u4e0b\u6587 <\/h3>\n<pre>function themetoggler() {\n    const { theme, toggletheme } = usecontext(themecontext); \/\/ access context values\n    return (\n        &lt;button onclick={toggletheme}&gt;\n            switch to {theme === 'light' ? 'dark' : 'light'} mode\n        &lt;\/button&gt;\n    );\n}\n\nfunction displaytheme() {\n    const { theme } = usecontext(themecontext);\n    return &lt;p&gt;current theme: {theme}&lt;\/p&gt;;\n}\n<\/pre>\n<h3> \u7b2c 3 \u6b65\uff1a\u5728\u4e3b\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u63d0\u4f9b\u7a0b\u5e8f <\/h3>\n<pre>function app() {\n    return (\n        &lt;themeprovider&gt;\n            &lt;displaytheme \/&gt;\n            &lt;themetoggler \/&gt;\n        &lt;\/themeprovider&gt;\n    );\n}\n\nexport default app;\n<\/pre>\n<p>\u73b0\u5728\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5355\u51fb\u6309\u94ae\u5728\u6d45\u8272\u548c\u6df1\u8272\u4e3b\u9898\u4e4b\u95f4\u5207\u6362\uff0c\u4e3b\u9898\u72b6\u6001\u4f1a\u663e\u793a\u5728\u65c1\u8fb9\u3002\u8be5\u9879\u76ee\u6f14\u793a\u4e86 usecontext \u5982\u4f55\u5141\u8bb8\u591a\u4e2a\u7ec4\u4ef6\u5171\u4eab\u5168\u5c40\u72b6\u6001\u66f4\u6539\u5e76\u5bf9\u5168\u5c40\u72b6\u6001\u66f4\u6539\u505a\u51fa\u53cd\u5e94\u3002<\/p>\n<hr>\n<h2> \u8ff7\u4f60\u9879\u76ee 2\uff1a\u7ba1\u7406\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1 <\/h2>\n<p>\u5bf9\u4e8e\u7b2c\u4e8c\u4e2a\u9879\u76ee\uff0c\u8ba9\u6211\u4eec\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u4f7f\u7528 usecontext \u8ddf\u8e2a\u7528\u6237\u7684\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\u3002<\/p>\n<h3> \u7b2c 1 \u6b65\uff1a\u521b\u5efa\u8eab\u4efd\u9a8c\u8bc1\u4e0a\u4e0b\u6587 <\/h3>\n<pre>import react, { createcontext, usecontext, usestate } from 'react';\n\nconst authcontext = createcontext();\n\nexport function authprovider({ children }) {\n    const [isauthenticated, setisauthenticated] = usestate(false);\n\n    const login = () =&gt; setisauthenticated(true);\n    const logout = () =&gt; setisauthenticated(false);\n\n    return (\n        &lt;authcontext.provider value={{ isauthenticated, login, logout }}&gt;\n            {children}\n        &lt;\/authcontext.provider&gt;\n    );\n}\n<\/pre>\n<h3> \u7b2c2\u6b65\uff1a\u521b\u5efa\u767b\u5f55\u548c\u6ce8\u9500\u7ec4\u4ef6 <\/h3>\n<pre>function loginbutton() {\n    const { login } = usecontext(authcontext); \/\/ access login function\n    return &lt;button onclick={login}&gt;login&lt;\/button&gt;;\n}\n\nfunction logoutbutton() {\n    const { logout } = usecontext(authcontext); \/\/ access logout function\n    return &lt;button onclick={logout}&gt;logout&lt;\/button&gt;;\n}\n<\/pre>\n<h3> \u6b65\u9aa43\uff1a\u663e\u793a\u7528\u6237\u72b6\u6001 <\/h3>\n<pre>function userstatus() {\n    const { isauthenticated } = usecontext(authcontext);\n    return (\n        &lt;p&gt;{isauthenticated ? 'welcome back!' : 'please log in.'}&lt;\/p&gt;\n    );\n}\n<\/pre>\n<h3> \u7b2c 4 \u6b65\uff1a\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7ec4\u5408\u7ec4\u4ef6 <\/h3>\n<pre>function App() {\n    return (\n        &lt;AuthProvider&gt;\n            &lt;UserStatus \/&gt;\n            &lt;LoginButton \/&gt;\n            &lt;LogoutButton \/&gt;\n        &lt;\/AuthProvider&gt;\n    );\n}\n\nexport default App;\n<\/pre>\n<p>\u73b0\u5728\uff0c\u60a8\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\u5e94\u7528\u7a0b\u5e8f\uff0c\u5176\u4e2d\u767b\u5f55\u548c\u6ce8\u9500\u6309\u94ae\u53ef\u4ee5\u66f4\u65b0\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u7528\u6237\u72b6\u6001\u3002\u8be5\u9879\u76ee\u6f14\u793a\u4e86 usecontext \u5982\u4f55\u5728\u73b0\u5b9e\u573a\u666f\u4e2d\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u7684\u72b6\u6001\u3002<\/p>\n<hr>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u901a\u8fc7\u8fd9\u4e24\u4e2a\u9879\u76ee\uff0c\u60a8\u5df2\u7ecf\u4e86\u89e3\u4e86 usecontext \u5982\u4f55\u7b80\u5316\u7ec4\u4ef6\u4e4b\u95f4\u7684\u6570\u636e\u5171\u4eab\uff0c\u800c\u65e0\u9700\u8fdb\u884c prop \u94bb\u53d6\u3002\u4e3b\u9898\u5207\u6362\u5668\u548c\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\u9879\u76ee\u4e3a\u6709\u6548\u7ba1\u7406\u5168\u5c40\u72b6\u6001\u63d0\u4f9b\u4e86\u5b9e\u7528\u7684\u89c1\u89e3\u3002\u65e0\u8bba\u60a8\u662f\u5207\u6362\u4e3b\u9898\u8fd8\u662f\u5904\u7406\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\uff0cusecontext \u90fd\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u6765\u6784\u5efa\u9ad8\u6548\u4e14\u6709\u7ec4\u7ec7\u7684\u5e94\u7528\u7a0b\u5e8f\u3002 <\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300auseContext\uff1aReact Hooks\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n<p>      \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb\u5220\u9664    <\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Python \u7684 format() \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u53d8\u91cf\u8868\u8fbe\u5f0f\u52a8\u6001\u6307\u5b9a\u53c2\u6570\u7f16\u53f7\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>useContext\uff1aReact Hooks \u6536\u85cf \u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u52aa\u529b\u5b66\u4e60\u5427\uff01\u672c\u6587\u300auseContext\uff1aReact Hooks\u300b\u4e3b\u8981\u8bb2\u89e3\u4e86\u7b49\u7b49\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6211\u4f1a\u5728\u7c73\u4e91\u4e2d\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u7684\u7cfb\u5217\u6587\u7ae0\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u5e76\u79ef\u6781\u7559\u8a00\u5efa\u8bae\u3002\u4e0b\u9762\u5c31\u5148\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u672c\u7bc7\u6b63\u6587\u5185\u5bb9\u5427\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01 react \u4e2d\u7684 usecontext \u548c\u4e24\u4e2a\u5b9e\u7528\u7684\u8ff7\u4f60\u9879\u76ee \u4ecb\u7ecd \u5728 react \u4e2d\uff0c\u7ba1\u7406\u7ec4\u4ef6\u4e4b\u95f4\u7684\u6570\u636e\u81f3\u5173\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5728\u591a\u4e2a\u7ec4\u4ef6\u9700\u8981\u8bbf\u95ee\u76f8\u540c\u6570\u636e\u7684\u5927\u578b\u5e94\u7528\u7a0b\u5e8f\u4e2d\u3002 prop \u94bb\u53d6\uff08\u5c06 props \u4f20\u9012\u5230\u7ec4\u4ef6\u6811\u7684\u591a\u4e2a\u7ea7\u522b\uff09\u5f88\u5feb\u5c31\u4f1a\u53d8\u5f97\u5f88\u9ebb\u70e6\u3002\u8fd9\u5c31\u662f react \u7684 usecontext hook \u7684\u95ea\u5149\u70b9\u3002 usecontext \u5141\u8bb8\u60a8\u8de8\u7ec4\u4ef6\u5171\u4eab\u6570\u636e\uff0c\u800c\u65e0\u9700\u624b\u52a8\u4f20\u9012 props\uff0c\u8fd9\u4f7f\u5176\u6210\u4e3a\u72b6\u6001\u7ba1\u7406\u7684\u5b9d\u8d35\u5de5\u5177\u3002 \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u9996\u5148\u8be6\u7ec6\u89e3\u91ca usecontext\u3001\u5176\u8bed\u6cd5\u53ca\u5176\u4f18\u70b9\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u6784\u5efa\u4e24\u4e2a\u8ff7\u4f60\u9879\u76ee\u6765\u5de9\u56fa\u8fd9\u79cd\u7406\u89e3\uff1a \u4e3b\u9898\u5207\u6362\u5668\uff1a\u4e00\u4e2a\u7b80\u5355\u7684\u4e3b\u9898\u5207\u6362\u5668\uff0c\u7528\u4e8e\u5c55\u793a usecontext \u5982\u4f55\u7ba1\u7406\u5168\u5c40\u72b6\u6001\u3002 \u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u72b6\u6001\uff1a\u5904\u7406\u7528\u6237\u767b\u5f55\u72b6\u6001\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u6f14\u793a\u5982\u4f55\u5728\u5b9e\u9645\u7528\u4f8b\u4e2d\u4f7f\u7528 usecontext\u3002 \u5728\u672c\u6559\u7a0b\u7ed3\u675f\u65f6\uff0c\u60a8\u5c06\u5177\u5907\u5728\u4efb\u4f55 react \u9879\u76ee\u4e2d\u81ea\u4fe1\u5730\u4f7f\u7528 usecontext \u7684\u80fd\u529b\u3002 \u4ec0\u4e48\u662fusecontext\uff1f usecontext \u7684\u57fa\u7840\u77e5\u8bc6 usecontext \u662f\u4e00\u4e2a react hook\uff0c\u5141\u8bb8\u7ec4\u4ef6\u76f4\u63a5\u8ba2\u9605\u4e0a\u4e0b\u6587\u3002\u5b83\u4f7f\u7ec4\u4ef6\u80fd\u591f\u4ece\u7ec4\u4ef6\u6811\u4e2d\u4f4d\u4e8e\u5176\u4e0a\u65b9\u7684\u6700\u8fd1\u7684\u63d0\u4f9b\u7a0b\u5e8f\u8bbf\u95ee\u5168\u5c40\u6570\u636e\uff0c\u4ece\u800c\u6709\u52a9\u4e8e\u907f\u514d prop \u94bb\u53d6\u7684\u9ebb\u70e6\u3002 usecontext \u7684\u8bed\u6cd5 \u4ee5\u4e0b\u662f\u521b\u5efa\u548c\u4f7f\u7528\u4e0a\u4e0b\u6587\u7684\u57fa\u672c\u8bed\u6cd5\uff1a import react, { usecontext, createcontext } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-53420","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=53420"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/53420\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=53420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=53420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=53420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}