Replies: 2 comments 3 replies
-
Apologize for the long story, but I'd like to show my findings, so future adventurers might find it useful when they are trying to use Initially I have created something like this: ...
// fetch config is basically the parentId and a cursor in an object { parentId, searchAfter }
const [fetchConfig, setFetchConfig] = useState(initialFetchConfig)
const previousFetchConfig = usePrevious(fetchConfig)
// the query needs to alter the currently cached state and return that, since we are only partially updating the tree state
const fetchTreeData = (key, variables) => {
const state = queryCache.getQueryData(key) as Map<string, any>
console.log('Fetching', variables)
return fetchNodes(state, query, variables, limit)
}
const { status, data: rawTreeData, error, refetch } = useQuery({
queryKey: treeId, // unique key that identifies the state of the entire tree
queryFn: fetchTreeData,
variables: [fetchConfig]
})
// this is a workaround to supply additional parameters to the query and reuse the currently cached state, refetch unfortunately does not support optional query variables to be passed
useEffect(() => {
if (previousFetchConfig !== fetchConfig) {
refetch()
}
})
// on expansion we set the next fetch config
const onExpandNode = (node) => {
const { parentId, searchAfter } = node
setFetchConfig([{ parentId, searchAfter }])
// it would be nice to use refetch here to return the promse for the tree so it won't stop the loading animation, but we cannot supply query parameters
return Promise.resolve()
}
if (status === 'loading') {
return <Spinner />
} else {
// this just converts the remote data to the Tree UI model
const treeData = mapToTreeData(rawTreeData)
return (
<Tree
showIcon
defaultExpandedKeys={defaultExpandedKeys}
loadData={(node) => onExpandNode(node)}
treeData={treeData}
/>
)
} This is working, but the loading animations supported by the tree do not work (because of the resolved Promise I need to send back from To overcome the animation issue, I dug a bit into the const onExpandNode = (node) => {
const { parentId, searchAfter } = node
// this is basically a workaround to call my 'queryFn' function but with an updated set of variables that should not be part of the queryKey.
return refetch({ __queryFn: () => fetchTreeData(treeId, [{ parentId, searchAfter }]) }).then(() => {})
} I know Would it be possible to pass new optional query parameters to Let me know what you think. Feel free to share. |
Beta Was this translation helpful? Give feedback.
-
In fact, there is a more appropriate solution using option
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi All,
Is it possible to use react-query for hierarchical data?
My data structure is something like this:
The API has a single endpoint
GET /items
to retrieve a list of items along with a cursor (after
), the number of items requestedlimit
and the total number of items that match the query.The endpoint allows filtering by
id
,parent
and any other propertyItem
has.Basically what I'd like to achieve is to replace the current Fetch API solution to use
react-query
instead. The content will be rendered in an Ant.Design Tree, rc-tree, react-virtualized-tree or equivalent.To even simplify the idea and remove the hierarchical nature, I think this can be achieved by using multiple
useInfiniteQuery
hooks (one hook per parent item list, queryKey is theparentId
and theafter
value is the cursor), but unfortunately dynamic number of hooks is not allowed in React (unless I'm missing something).Any help will be much appreciated,
Mark
Beta Was this translation helpful? Give feedback.
All reactions