reactjs - 无法对未安装的组件执行 React 状态更新。无状态
问题描述
让我们假装一会儿,我知道错误的含义以及通常是什么时候产生的,但现在我不知道反应抱怨什么。
错误来自这个简单的组件:
export interface PostHandlerOutProps {
posts: PostFragment[];
}
export interface PostHandlerProps {
children: (outProps: PostHandlerOutProps) => ReactNode;
initialPosts?: PostHandlerOutProps['posts'];
}
const PostsHandler: FC<PostHandlerProps> = ({ children, initialPosts = [] }) => {
const { data } = useQuery<PostsQuery, PostsQueryVariables>(postsQuery);
return <>{children({ posts: data?.posts || initialPosts })}</>;
};
以下是该组件的使用方式:
export interface PostsProps {
initialPosts: PostFragment[];
}
const Posts: FC<PostsProps> = ({ initialPosts }) => {
return (
<PostsHandler initialPosts={initialPosts}>
{({ posts }) => (
<div>For simplicity sake nothing but I am still getting the error.</div>
)}
</PostsHandler>
);
};
我仍然随机收到此错误:
Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.
at PostsHandler (webpack-internal:///./src/components/post/PostsHandler.tsx:20:3)
at Posts (webpack-internal:///./src/views/admin/Posts.tsx:18:3)
有人知道这里发生了什么吗?这是我应该关心的事情吗?
只是为了解释。我需要这种架构,因为 initialPosts 来自服务器端渲染,我需要在客户端替换相同的数据,但来自 useQuery,因为它使我能够对突变的阿波罗缓存更新做出反应。
解决方案
好的,看来我设法确定了 Apollo hook 的问题useQuery
。由于 2019 年开发人员抱怨这个问题,现在看来 Apollo 团队最终会解决这个问题。根据这篇文章:https ://github.com/apollographql/apollo-client/issues/7404#issuecomment-776142370 - 他们会静默警告。布拉沃阿波罗团队!
推荐阅读
- python - 我是否在此代码中正确考虑了夏令时?
- django - Thinkific和多重登录预防
- javascript - 如何显示过渡,当我将鼠标悬停在按钮上时,主 div 将展开显示每个按钮
- html - 使用 MS Graph 时添加到我的文件上传中的额外内容
- amazon-web-services - AWS API Gateway 偶尔会出现 4xx 错误
- elasticsearch - ElasticSearch:使用单个索引与区分大小写和不区分大小写的搜索进行部分匹配
- python - 如何返回 base64Encoded Matplotlib 图表?
- subprocess - Python:逐行捕获子进程输出,\n和\r都算作换行符?
- php - 如何在 Laravel 8 中编译资产?
- sublimetext - 在最近的崇高文本更新后光标特别高并且不闪烁