React上下文DefaultValue在组件树周围没有提供的提供商时返回未定义

发布于 2025-01-17 11:45:37 字数 3767 浏览 0 评论 0原文

我一直在阅读React文档以刷新我的概念。阅读时,我在上下文部分中遇到了此声明:

传递给函数的值参数将等于值 在树上的此上下文中最接近的提供商的道具。如果 上面没有此上下文的提供者,价值参数将 等于传递给createContext()的默认值。

因此,自然而然地,我创建了一些片段来实际测试场景。

我的入口点(app.js):

import "./styles.css";
import WearLacoste from "./wearLacoste";
import WearPrada from "./wearPrada";
import OutFitProvider from "./outfitContext";

export default function App() {
  return (
    <div className="App">
      <h1>What to wear?</h1>
      <OutFitProvider>
        <WearPrada />
      </OutFitProvider>
      <WearLacoste />
    </div>
  );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

请注意,我有两个组件。这些组件返回在浏览器上呈现的绳子,说明要穿哪种衣服。组件名称&lt; wearprada/&gt;是由提供商包裹的,另一个(&lt; wearlacoste/&gt;)不是要测试> DefaultValue方案。

以下是我的上下文文件(OffitContext.js):

import React, { useContext, useState } from "react";

const MyOutfitContext = React.createContext("Lacoste");

//a custom hook to consume context
export const useOutfit = () => {
  return useContext(MyOutfitContext);
};

const OutfitContextProvider = ({ children }) => {
  const [outfit, setOutfit] = useState("Prada");

  return (
    <MyOutfitContext.Provider value={{ outfit }}>
      {children}
    </MyOutfitContext.Provider>
  );
};

export default OutfitContextProvider;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

最后,我的两个组件都做了完全相同的事情。

Wearprada.js:

import { useOutfit } from "./outfitContext";

const WearPrada = () => {
  const { outfit } = useOutfit();
  console.log("Outfit expects Prada", outfit);
  return <h1>Wearing {outfit} RN </h1>;
};

export default WearPrada;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

wearlacoste.js:

import { useOutfit } from "./outfitContext";

const WearLacoste = () => {
  const { outfit } = useOutfit();
  console.log("Outfit expects Lacoste", outfit);
  return <h1>Wearing {outfit} RN </h1>;
};

export default WearLacoste;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

我的问题是:

包裹在提供商中的组件的行为是预期的。它获得 state 值在上下文中初始化。但是,正如文档中所声称的那样,未包装在提供商中的组件仍然获得不确定的值,而不是defaultValuelacoste .js)。我在这里做错了吗?事先感谢您的帮助。

I have been reading react docs to refresh my concepts. While reading, I came across this statement in context section:

The value argument passed to the function will be equal to the value
prop of the closest Provider for this context above in the tree. If
there is no Provider for this context above, the value argument will
be equal to the defaultValue that was passed to createContext().

So naturally, I created some snippets to actually test the scenario.

My entry point (App.js):

import "./styles.css";
import WearLacoste from "./wearLacoste";
import WearPrada from "./wearPrada";
import OutFitProvider from "./outfitContext";

export default function App() {
  return (
    <div className="App">
      <h1>What to wear?</h1>
      <OutFitProvider>
        <WearPrada />
      </OutFitProvider>
      <WearLacoste />
    </div>
  );
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Notice that I have two components. These components return a string rendered on the browser, telling which dress to wear. One of the component name <WearPrada /> is wrapped by the provider, the other (<WearLacoste/>) is not, to test the defaultValue scenario.

Following is my context file (outfitContext.js):

import React, { useContext, useState } from "react";

const MyOutfitContext = React.createContext("Lacoste");

//a custom hook to consume context
export const useOutfit = () => {
  return useContext(MyOutfitContext);
};

const OutfitContextProvider = ({ children }) => {
  const [outfit, setOutfit] = useState("Prada");

  return (
    <MyOutfitContext.Provider value={{ outfit }}>
      {children}
    </MyOutfitContext.Provider>
  );
};

export default OutfitContextProvider;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

And finally both of my components, which do the exact same thing.

wearPrada.js :

import { useOutfit } from "./outfitContext";

const WearPrada = () => {
  const { outfit } = useOutfit();
  console.log("Outfit expects Prada", outfit);
  return <h1>Wearing {outfit} RN </h1>;
};

export default WearPrada;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

wearLacoste.js :

import { useOutfit } from "./outfitContext";

const WearLacoste = () => {
  const { outfit } = useOutfit();
  console.log("Outfit expects Lacoste", outfit);
  return <h1>Wearing {outfit} RN </h1>;
};

export default WearLacoste;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

My problem is:

The component wrapped in the provider is behaving as expected. It gets the state value initialised in context. But as claimed in the document, the component not wrapped in the provider still gets an undefined value instead of the defaultValue which is Lacoste (see outfitContext.js). Am I doing anything wrong here? Thanks in advance for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻之岛 2025-01-24 11:45:37

看来您已将对象{oftfit:string}与简单的字符串混合。上下文值有望是任何地方的对象,但是在设置默认值时,您使用字符串。

我相信您想要的是const myoutfitContext = react.CreateContext({offit:“ lacoste”});

It seems that you have mixed the object { outfit: string } with a simple string. The context value is expected to be an object everywhere but when setting the default value you use a string.

I believe what you want is const MyOutfitContext = React.createContext({ outfit: "Lacoste" });

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文