[英]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.