简体   繁体   English

React - value 属性是必需的<Context.Provider> . 你是拼错了还是忘记通过了?

[英]React - The value prop is required for the <Context.Provider>. Did you misspell it or forget to pass it?

I have implemented a React context which listens to one document of my database when it is mounted.我已经实现了一个 React 上下文,它在挂载时监听我的数据库的一个文档。

function AccountStateProvider({ currentUser, children }) {
  const { signOut } = useSignOut();

  /**
   * Handles the current user's account deletion, signing out from the active
   * session.
   *
   * @returns {void} Nothing.
   */
  const handleOnAccountDeleted = () => {
    signOut(false);
  };

  useEffect(() => {
    if (!currentUser.data?.id) {
      return undefined;
    }

    //
    // Forces the logout of all active sessions on different mobile
    // devices after the user's account deletion.
    //
    const unsubscribe = onAccountDeleted(
      currentUser.data.id,
      handleOnAccountDeleted
    );

    return () => {
      unsubscribe();
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [currentUser.data?.id]);

  return (
    <AccountStateContext.Provider>
      {children}
    </AccountStateContext.Provider>
  );
}

My purpose is to listen to this document in a global component which wraps the entire app... and I think that Context is a good tool for my use case.我的目的是在一个包含整个应用程序的全局组件中收听这个文档……我认为 Context 是我用例的好工具。

The problem is that I am getting this warning:问题是我收到了这个警告:

Warning: The value prop is required for the <Context.Provider> .警告: <Context.Provider>需要value属性。 Did you misspell it or forget to pass it?你是拼错了还是忘记通过了?

Is it 'illegal' to use context this way without passing any value to the consumers?在不向消费者传递任何价值的情况下以这种方式使用上下文是否“非法”?

This would likely be better suited as a custom hook.这可能更适合作为自定义钩子。 Here's an example implementation:这是一个示例实现:

function useAccountState(currentUser) {
  const { signOut } = useSignOut();

  /**
   * Handles the current user's account deletion, signing out from the active
   * session.
   *
   * @returns {void} Nothing.
   */
  const handleOnAccountDeleted = () => {
    signOut(false);
  };

  useEffect(() => {
    if (!currentUser.data?.id) {
      return undefined;
    }

    //
    // Forces the logout of all active sessions on different mobile
    // devices after the user's account deletion.
    //
    const unsubscribe = onAccountDeleted(
      currentUser.data.id,
      handleOnAccountDeleted
    );

    return () => {
      unsubscribe();
    };
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [currentUser.data?.id]);

  return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React context.provider不会更改默认值 - React context.provider doesn't change the default value 在 React 中将多个值和设置器对传递给 Context.Provider - Passing multiple value and setter pairs to Context.Provider in React 为什么 React Context.Provider 是必需的(或有用的)? - Why Is React Context.Provider Necessary (Or useful)? React Native Context - 从渲染之外的 Context.provider 中检索值 function - React Native Context - retrieve the value from Context.provider outside of the render function 如何访问react Context.Provider值内的另一个函数? - How can I access another function inside the react Context.Provider value? React 将“this”传递给上下文提供者值 - React pass "this" to context provider value 如何在 react 和 typescript 中使用 context.provider 设置 state? - How to set the state using context.provider in react and typescript? 如何创建类似“ context.Provider” /“ context.Consumer”的结构以在机器人应用中传递值? - How to create a `context.Provider`/`context.Consumer`-like structure to pass values in a bot app? 从 Consumer -&gt; Provider 上下文传递 prop 并在 Provider 渲染之外访问它? - Pass prop from Consumer -> Provider context and access it outside of Provider render? 我应该将反应钩子作为值传递给上下文提供者吗? - Should i pass react hooks as a value to Context Provider?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM