如何映射反应查询的依赖查询的数据
How to map data of react query's dependent query
使用 react-query
v.3,获取 useQuery()
依赖查询的承诺响应数据的最佳方法是什么?
我不能map()
直接对数据响应。
参考:https://react-query.tanstack.com/guides/dependent-queries
- 您可以通过
isSuccess
检查数据是否可用:
const { isSuccess, data } = useQuery(
['projects', userId],
getProjectsByUser,
{
// The query will not execute until the userId exists
enabled: !!userId,
}
)
if (isSuccess) {
return data.map(...) // <-- data is guaranteed to be defined
}
- 您可以简单地检查数据是否存在,或者使用 optional chaining:
const { isSuccess, data } = useQuery(
['projects', userId],
getProjectsByUser,
{
// The query will not execute until the userId exists
enabled: !!userId,
}
)
return data?.map(...) ?? null // <-- make sure to handle fallback with ??
使用 react-query
v.3,获取 useQuery()
依赖查询的承诺响应数据的最佳方法是什么?
我不能map()
直接对数据响应。
参考:https://react-query.tanstack.com/guides/dependent-queries
- 您可以通过
isSuccess
检查数据是否可用:
const { isSuccess, data } = useQuery(
['projects', userId],
getProjectsByUser,
{
// The query will not execute until the userId exists
enabled: !!userId,
}
)
if (isSuccess) {
return data.map(...) // <-- data is guaranteed to be defined
}
- 您可以简单地检查数据是否存在,或者使用 optional chaining:
const { isSuccess, data } = useQuery(
['projects', userId],
getProjectsByUser,
{
// The query will not execute until the userId exists
enabled: !!userId,
}
)
return data?.map(...) ?? null // <-- make sure to handle fallback with ??