reactjs - 对所有查询都使用相同的功能 onSuccess raect-query
问题描述
我有一个用例,我想在onSuccess
全局范围内为所有突变和查询运行相同的函数,而不必为每个单独的查询设置相同的函数(我有很多查询)
我有很多这样的查询
const q1 = useQuery(
"q1",
async () => {
return await axios
.get(`/some/path`)
.then((res) => res.data)
.catch((e) => CustomError(e));
},
{
onSuccess: () => generic(),
}
);
const q2 = useQuery(
"q2",
async () => {
return await axios
.get(`/some/path`)
.then((res) => res.data)
.catch((e) => CustomError(e));
},
{
onSuccess: () => generic(),
}
);
const q1 = useQuery(
"q3",
async () => {
return await axios
.get(`/some/path`)
.then((res) => res.data)
.catch((e) => CustomError(e));
},
{
onSuccess: () => generic()
}
);
function generic() {
return "should be set globally and run on ever OnSuccess event"
}
但是我想为所有查询全局设置这个,像这样
const queryCache = new QueryClient({
defaultConfig: {
queries: {
onSuccess: () => {
return "should be set globally and run on ever OnSuccess event";
},
},
},
});
const q1 = useQuery("q1", async () => {
return await axios
.get(`/some/path`)
.then((res) => res.data)
.catch((e) => CustomError(e));
});
const q2 = useQuery("q2", async () => {
return await axios
.get(`/some/path`)
.then((res) => res.data)
.catch((e) => CustomError(e));
});
const q1 = useQuery("q3", async () => {
return await axios
.get(`/some/path`)
.then((res) => res.data)
.catch((e) => CustomError(e));
});
我已经为此类功能搜索了大约一个小时的文档,但找不到任何东西
解决方案
有一个针对该确切用例的公开 PR:https ://github.com/tannerlinsley/react-query/pull/2404
它增加了在 queryCache 上进行全局 onSuccess 回调的可能性。
推荐阅读
- excel - 获取工作簿中所有子的名称
- javascript - 如果根据复选框值选中或取消选中复选框,则下拉列表的值
- html - 为什么悬停颜色不填充
- java - Maven:如何处理包含模块的模块?
- java - 如何从线程转储报告中找到卡住线程的根本原因?
- rest-assured-jsonpath - 有效的 JSONPath 在 RestAssured JsonPath 中抛出 EOF
- excel - 如何使用 vba 从文本文件中提取特定的数据行
- javascript - 选择下拉值需要同时获取列表框中的下拉值
- laravel - SQLSTATE [42000]:语法错误或访问冲突:1055 SELECT 列表的表达式 #1 不在 GROUP BY 子句中,并且
- excel - 通过 VBA 在 IE 上使用小部件