{"id":88244,"date":"2024-04-07T17:26:38","date_gmt":"2024-04-07T09:26:38","guid":{"rendered":""},"modified":"2024-04-07T17:26:39","modified_gmt":"2024-04-07T09:26:39","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8react%e4%b8%ad%e4%bd%bf%e7%94%a8context-api","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/88244.html","title":{"rendered":"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Context API"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/02172643\/8b39aea3-3459-42ee-b39f-082fe9a4c1f6.webp\" alt=\"\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Context API\" \/><\/p>\n<p><p>\u5f53\u6211\u4eec\u8ba8\u8bba\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Context API\u65f6\uff0c\u5173\u952e\u70b9\u5305\u62ec<strong>\u7406\u89e3Context API\u7684\u4f5c\u7528\u3001\u521b\u5efaContext\u3001\u63d0\u4f9bContext<\/strong>\u4ee5\u53ca<strong>\u6d88\u8d39Context<\/strong>\u3002\u9996\u5148\uff0c<strong>Context API\u7684\u4f5c\u7528<\/strong>\u662f\u5141\u8bb8\u4f60\u5171\u4eab\u7279\u5b9a\u7684\u6570\u636e\uff08\u4f8b\u5982\u5f53\u524d\u7684\u8bed\u8a00\u8bbe\u7f6e\u3001UI\u4e3b\u9898\uff09\u8de8\u8d8a\u591a\u4e2a\u7ec4\u4ef6\uff0c\u800c\u4e0d\u5fc5\u663e\u5f0f\u5730\u901a\u8fc7\u6bcf\u4e2a\u7ec4\u4ef6\u9010\u5c42\u4f20\u9012props\u3002\u8fd9\u5bf9\u4e8e\u50cf\u4e3b\u9898\u914d\u7f6e\u6216\u7528\u6237\u8ba4\u8bc1\u8fd9\u6837\u7684\u5168\u5c40\u6570\u636e\u5c24\u5176\u6709\u7528\u3002\u4f8b\u5982\uff0c\u5728\u5b9e\u73b0\u4e00\u4e2a\u4e3b\u9898\u5207\u6362\u529f\u80fd\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7Context API\u8ba9\u6240\u6709\u7ec4\u4ef6\u90fd\u80fd\u63a5\u89e6\u5230\u5f53\u524d\u4e3b\u9898\u7684\u72b6\u6001\uff0c\u800c\u65e0\u9700\u5c06\u4e3b\u9898\u72b6\u6001\u548c\u51fd\u6570\u901a\u8fc7\u6bcf\u4e2a\u5355\u72ec\u7684\u7ec4\u4ef6\u4f20\u9012\u4e0b\u53bb\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728React\u5e94\u7528\u4e2d\u5b9e\u73b0\u548c\u4f7f\u7528Context API\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3CONTEXT API<\/h3>\n<\/p>\n<p><p>Context API\u662fReact\u81ea16.3\u7248\u672c\u5f15\u5165\u7684\u65b0\u7279\u6027\uff0c\u65e8\u5728\u89e3\u51b3\u7ec4\u4ef6\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u95ee\u9898\u3002\u5728\u6ca1\u6709Context API\u7684\u60c5\u51b5\u4e0b\uff0c\u6570\u636e\u53ea\u80fd\u901a\u8fc7props\u4f20\u9012\uff0c\u8fd9\u5728\u591a\u5c42\u5d4c\u5957\u7ec4\u4ef6\u7684\u60c5\u51b5\u4e0b\u4f1a\u53d8\u5f97\u975e\u5e38\u9ebb\u70e6\uff0c\u5373\u6240\u8c13\u7684\u201cprops drilling\u201d\u3002Context API\u7684\u5f15\u5165\uff0c\u4f7f\u5f97\u6211\u4eec\u80fd\u591f\u8de8\u7ec4\u4ef6\u5c42\u7ea7\u76f4\u63a5\u4f20\u9012\u6570\u636e\uff0c\u6781\u5927\u5730\u7b80\u5316\u4e86\u6570\u636e\u4f20\u9012\u7684\u590d\u6742\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u521b\u5efaCONTEXT<\/h3>\n<\/p>\n<p><p>\u8981\u5728React\u5e94\u7528\u4e2d\u4f7f\u7528Context API\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2aContext\u3002\u8fd9\u901a\u8fc7\u8c03\u7528<code>React.createContext<\/code>\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5e76\u4e14\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u9ed8\u8ba4\u503c\u3002\u521b\u5efaContext\u540e\uff0c\u4f60\u5c06\u83b7\u5f97\u4e24\u4e2a\u7ec4\u4ef6\uff1a<code>Provider<\/code>\u548c<code>Consumer<\/code>\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-jsx\">import React from &#039;react&#039;;<\/p>\n<p>const ThemeContext = React.createContext(&#039;light&#039;); \/\/ \u9ed8\u8ba4\u503c\u4e3a&#039;light&#039;<\/p>\n<p>export default ThemeContext;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3a<code>ThemeContext<\/code>\u7684Context\uff0c\u5e76\u4e14\u5b83\u7684\u9ed8\u8ba4\u503c\u662f&#039;light&#039;\uff0c\u6697\u793a\u4e86\u6709\u4e00\u4e2a\u5149\u660e\u7684\u4e3b\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u63d0\u4f9bCONTEXT<\/h3>\n<\/p>\n<p><p>\u521b\u5efaContext\u540e\uff0c\u5fc5\u987b\u901a\u8fc7<code>Provider<\/code>\u7ec4\u4ef6\u63d0\u4f9bContext\u503c\u4ee5\u4f9b\u5176\u4ed6\u7ec4\u4ef6\u6d88\u8d39\u3002<code>Provider<\/code>\u7ec4\u4ef6\u63a5\u6536\u4e00\u4e2a<code>value<\/code>\u5c5e\u6027\uff0c\u8fd9\u4e2a\u5c5e\u6027\u53ef\u4ee5\u662f\u4efb\u4f55\u4f60\u5e0c\u671b\u7531\u4e0b\u9762\u7684\u7ec4\u4ef6\u6811\u8bbf\u95ee\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-jsx\">import React from &#039;react&#039;;<\/p>\n<p>import ThemeContext from &#039;.\/ThemeContext&#039;;<\/p>\n<p>class App extends React.Component {<\/p>\n<p>  render() {<\/p>\n<p>    return (<\/p>\n<p>      &lt;ThemeContext.Provider value=&quot;dark&quot;&gt;<\/p>\n<p>        &lt;Toolbar \/&gt;<\/p>\n<p>      &lt;\/ThemeContext.Provider&gt;<\/p>\n<p>    );<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p>function Toolbar(props) {<\/p>\n<p>  return (<\/p>\n<p>    &lt;div&gt;<\/p>\n<p>      &lt;ThemedButton \/&gt;<\/p>\n<p>    &lt;\/div&gt;<\/p>\n<p>  );<\/p>\n<p>}<\/p>\n<p>function ThemedButton(props) {<\/p>\n<p>  return (<\/p>\n<p>    &lt;ThemeContext.Consumer&gt;<\/p>\n<p>      {theme =&gt; &lt;Button theme={theme} \/&gt;}<\/p>\n<p>    &lt;\/ThemeContext.Consumer&gt;<\/p>\n<p>  );<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7684<code>App<\/code>\u7ec4\u4ef6\u901a\u8fc7<code>ThemeContext.Provider<\/code>\u63d0\u4f9b\u4e00\u4e2avalue\u503c\u201cdark\u201d\uff0c\u8fd9\u8868\u793a\u6211\u4eec\u60f3\u8981\u4f7f\u7528\u4e00\u4e2a\u6697\u8272\u4e3b\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6d88\u8d39CONTEXT<\/h3>\n<\/p>\n<p><p>\u73b0\u5728Context\u5df2\u7ecf\u63d0\u4f9b\uff0c\u6211\u4eec\u9700\u8981\u5728\u7ec4\u4ef6\u6811\u4e2d\u7684\u4efb\u610f\u4f4d\u7f6e\u6d88\u8d39\uff08\u4f7f\u7528\uff09\u8fd9\u4e2aContext\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4e24\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528<code>Consumer<\/code>\u7ec4\u4ef6\u6216\u8005<code>contextType<\/code>\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528Consumer\u7ec4\u4ef6<\/strong><\/p>\n<\/p>\n<p><p><code>Consumer<\/code>\u7ec4\u4ef6\u662f\u4ece\u521b\u5efa\u7684Context\u5bf9\u8c61\u4e2d\u83b7\u53d6\u5230\u7684\uff0c\u7528\u4e8e\u4ece\u7ec4\u4ef6\u6811\u4e2d\u7684\u4efb\u4f55\u5730\u65b9\u8ba2\u9605\u5230Context\u7684\u53d8\u5316\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-jsx\">&lt;ThemeContext.Consumer&gt;<\/p>\n<p>  {theme =&gt; &lt;Button theme={theme} \/&gt;}<\/p>\n<p>&lt;\/ThemeContext.Consumer&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>ThemedButton<\/code>\u7ec4\u4ef6\u901a\u8fc7<code>ThemeContext.Consumer<\/code>\u83b7\u53d6Context\u4e2d\u63d0\u4f9b\u7684\u503c\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u81ea\u5df1\u7684props\u4f20\u9012\u7ed9<code>Button<\/code>\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528contextType<\/strong><\/p>\n<\/p>\n<p><p>\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>contextType<\/code>\u5c5e\u6027\u5728\u4efb\u4f55\u751f\u547d\u5468\u671f\u65b9\u6cd5\u4e2d\u8bbf\u95eeContext\u503c\u3002\u8fd9\u4e2a\u5c5e\u6027\u53ea\u80fd\u5728class\u7ec4\u4ef6\u4e2d\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-jsx\">class ThemedButton extends React.Component {<\/p>\n<p>  static contextType = ThemeContext;<\/p>\n<p>  render() {<\/p>\n<p>    return &lt;Button theme={this.context} \/&gt;;<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>ThemedButton<\/code>\u7ec4\u4ef6\u901a\u8fc7\u6307\u5b9a<code>contextType<\/code>\u4e3a<code>ThemeContext<\/code>\uff0c\u53ef\u4ee5\u5728\u4efb\u4f55\u5730\u65b9\u901a\u8fc7<code>this.context<\/code>\u8bbf\u95ee\u5230\u5f53\u524d\u7684Context\u503c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u66f4\u65b0CONTEXT<\/h3>\n<\/p>\n<p><p>\u867d\u7136\u53ef\u4ee5\u8ba2\u9605Context\u7684\u503c\uff0c\u4f46\u662f\u76f4\u5230\u73b0\u5728\uff0c\u6211\u4eec\u8fd8\u6ca1\u6709\u770b\u5230\u5982\u4f55\u66f4\u65b0Context\u4e2d\u7684\u503c\u3002\u66f4\u65b0Context\u9700\u8981\u5c06\u72b6\u6001\u63d0\u5347\u5230\u6700\u8fd1\u7684\u7236<code>Provider<\/code>\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-jsx\">class App extends React.Component {<\/p>\n<p>  state = {<\/p>\n<p>    theme: &#039;light&#039;<\/p>\n<p>  };<\/p>\n<p>  toggleTheme = () =&gt; {<\/p>\n<p>    this.setState(state =&gt; ({<\/p>\n<p>      theme: state.theme === &#039;light&#039; ? &#039;dark&#039; : &#039;light&#039;<\/p>\n<p>    }));<\/p>\n<p>  };<\/p>\n<p>  render() {<\/p>\n<p>    return (<\/p>\n<p>      &lt;ThemeContext.Provider value={this.state.theme}&gt;<\/p>\n<p>        &lt;Toolbar changeTheme={this.toggleTheme} \/&gt;<\/p>\n<p>      &lt;\/ThemeContext.Provider&gt;<\/p>\n<p>    );<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6dfb\u52a0\u4e86\u4e00\u4e2a\u65b9\u6cd5<code>toggleTheme<\/code>\u53bb\u66f4\u6539App\u7ec4\u4ef6\u7684\u72b6\u6001\u3002\u5f53\u6211\u4eec\u5728Provider\u7684<code>value<\/code>\u5c5e\u6027\u4e2d\u4f20\u9012\u8fd9\u4e2a\u72b6\u6001\u65f6\uff0c\u6240\u6709\u7684\u6d88\u8d39\u8005\u7ec4\u4ef6\u5c06\u63a5\u6536\u5230\u65b0\u7684Context\u503c\uff0c\u4ece\u800c\u89e6\u53d1\u91cd\u65b0\u6e32\u67d3\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528CONTEXT HOOKS<\/h3>\n<\/p>\n<p><p>\u5728React\u6700\u65b0\u7684\u7248\u672c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>useContext<\/code> Hook\u5728\u51fd\u6570\u7ec4\u4ef6\u4e2d\u66f4\u52a0\u65b9\u4fbf\u5730\u8ba2\u9605Context\uff0c\u800c\u4e0d\u9700\u8981\u4f7f\u7528<code>Consumer<\/code>\u7ec4\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-jsx\">import React, { useContext } from &#039;react&#039;;<\/p>\n<p>import ThemeContext from &#039;.\/ThemeContext&#039;;<\/p>\n<p>function ThemedButton() {<\/p>\n<p>  const theme = useContext(ThemeContext);<\/p>\n<p>  return &lt;Button theme={theme} \/&gt;;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>useContext<\/code> Hook \u5e76\u4f20\u5165\u6211\u4eec\u60f3\u8981\u8ba2\u9605\u7684Context\u5bf9\u8c61\uff08\u5373<code>ThemeContext<\/code>\uff09\uff0c\u5b83\u5c06\u8fd4\u56de\u5f53\u524dContext\u7684\u503c\uff0c\u8fd9\u4e2a\u503c\u548c\u4e00\u4e2a\u8fd1\u7684<code>Provider<\/code>\u7684<code>value<\/code>\u5c5e\u6027\u662f\u4e00\u81f4\u7684\u3002\u5982\u679cProvider\u66f4\u65b0\u4e86\u8fd9\u4e2a\u503c\uff0c\u90a3\u4e48\u4f7f\u7528<code>useContext<\/code>\u7684\u6240\u6709\u7ec4\u4ef6\u4e5f\u5c06\u91cd\u65b0\u6e32\u67d3\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u4f20\u9012\u5728React\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u8de8\u7ec4\u4ef6\u7ea7\u522b\u7684\u6570\u636e\u3002\u4e0d\u8bba\u662f\u5728\u5927\u578b\u7684\u5e94\u7528\u7a0b\u5e8f\u91cc\u7ba1\u7406\u5168\u5c40\u72b6\u6001\uff0c\u8fd8\u662f\u5728\u5c0f\u578b\u9879\u76ee\u4e2d\u907f\u514d\u65e0\u8c13\u7684props\u4f20\u9012\uff0cContext API\u90fd\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q\uff1aReact\u4e2d\u7684Context API\u6709\u4ec0\u4e48\u4f5c\u7528\uff1f<\/strong><br \/>\nA\uff1aReact\u7684Context API\u662f\u4e00\u79cd\u7528\u4e8e\u5728\u7ec4\u4ef6\u6811\u4e2d\u5171\u4eab\u6570\u636e\u7684\u65b9\u6cd5\u3002\u5b83\u901a\u8fc7\u5141\u8bb8\u6211\u4eec\u5c06\u6570\u636e\u4ece\u7236\u7ec4\u4ef6\u4f20\u9012\u5230\u5b50\u7ec4\u4ef6\u4e2d\u7684\u4efb\u610f\u6df1\u5c42\u6b21\u7ec4\u4ef6\uff0c\u6765\u89e3\u51b3\u7ec4\u4ef6\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u9012\u95ee\u9898\u3002\u4f7f\u7528Context API\u53ef\u4ee5\u907f\u514d\u9010\u5c42\u901a\u8fc7props\u5c06\u6570\u636e\u4f20\u9012\u4e0b\u53bb\u7684\u9ebb\u70e6\uff0c\u5e76\u4f7f\u7ec4\u4ef6\u4e4b\u95f4\u7684\u901a\u4fe1\u66f4\u52a0\u7075\u6d3b\u65b9\u4fbf\u3002<\/p>\n<p><strong>Q\uff1a\u5982\u4f55\u521b\u5efa\u4e00\u4e2aReact\u7684Context\uff1f<\/strong><br \/>\nA\uff1a\u8981\u521b\u5efa\u4e00\u4e2aReact\u7684Context\uff0c\u53ef\u4ee5\u4f7f\u7528React\u7684createContext()\u51fd\u6570\u8fdb\u884c\u521b\u5efa\u3002createContext()\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u521d\u59cb\u503c\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542bProvider\u548cConsumer\u7ec4\u4ef6\u7684\u5bf9\u8c61\u3002Provider\u7ec4\u4ef6\u7528\u4e8e\u63d0\u4f9b\u5171\u4eab\u7684\u6570\u636e\uff0cConsumer\u7ec4\u4ef6\u7528\u4e8e\u63a5\u6536\u5171\u4eab\u7684\u6570\u636e\u3002<\/p>\n<p><strong>Q\uff1a\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Context API\u4f20\u9012\u6570\u636e\uff1f<\/strong><br \/>\nA\uff1a\u9996\u5148\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aContext\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528React\u7684createContext()\u51fd\u6570\u521b\u5efa\u3002\u7136\u540e\uff0c\u5728\u63d0\u4f9b\u5171\u4eab\u6570\u636e\u7684\u7ec4\u4ef6\u4e2d\uff0c\u4f7f\u7528Provider\u7ec4\u4ef6\u5c06\u6570\u636e\u4f20\u9012\u7ed9\u540e\u4ee3\u7ec4\u4ef6\u3002\u63a5\u6536\u5171\u4eab\u6570\u636e\u7684\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u5728\u9700\u8981\u7684\u5730\u65b9\u4f7f\u7528Consumer\u7ec4\u4ef6\u6765\u83b7\u53d6\u5171\u4eab\u6570\u636e\u3002\u901a\u8fc7\u5728Consumer\u7ec4\u4ef6\u7684\u5185\u90e8\u4f7f\u7528\u51fd\u6570\u6216\u7c7b\u7ec4\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u5171\u4eab\u6570\u636e\uff0c\u5e76\u6839\u636e\u9700\u8981\u8fdb\u884c\u6e32\u67d3\u3002<\/p>\n<p><strong>Q\uff1a\u5728\u4f7f\u7528Context API\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5982\u4f55\u5904\u7406\u5728\u7ec4\u4ef6\u6811\u4e2d\u591a\u4e2aProvider\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\nA\uff1a\u5f53\u5728\u7ec4\u4ef6\u6811\u4e2d\u5b58\u5728\u591a\u4e2aProvider\u7684\u65f6\u5019\uff0cReact\u4f1a\u4f7f\u7528\u6700\u8fd1\u7684Provider\u63d0\u4f9b\u7684\u503c\u3002\u5f53\u67d0\u4e2a\u7ec4\u4ef6\u5728\u591a\u4e2aProvider\u7684\u8303\u56f4\u5185\u65f6\uff0c\u5b83\u5c06\u63a5\u6536\u5230\u4e0e\u5b83\u6700\u63a5\u8fd1\u7684Provider\u63d0\u4f9b\u7684\u503c\u3002\u5982\u679c\u67d0\u4e2aProvider\u66f4\u65b0\u4e86\u5b83\u7684\u503c\uff0c\u5c5e\u4e8e\u5b83\u8303\u56f4\u5185\u7684\u6240\u6709\u7ec4\u4ef6\u90fd\u5c06\u63a5\u6536\u5230\u65b0\u7684\u503c\u3002\u53ef\u4ee5\u901a\u8fc7\u5408\u7406\u5730\u7ec4\u7ec7Provider\u7684\u5c42\u6b21\u7ed3\u6784\uff0c\u6765\u786e\u4fdd\u6570\u636e\u80fd\u591f\u6309\u7167\u9884\u671f\u8fdb\u884c\u4f20\u9012\u548c\u66f4\u65b0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f53\u6211\u4eec\u8ba8\u8bba\u5982\u4f55\u5728React\u4e2d\u4f7f\u7528Context API\u65f6\uff0c\u5173\u952e\u70b9\u5305\u62ec\u7406\u89e3Context API\u7684\u4f5c\u7528\u3001\u521b\u5efaCo [&hellip;]","protected":false},"author":3,"featured_media":88248,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/88244"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=88244"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/88244\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/88248"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=88244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=88244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=88244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}