Update without making network calls #10
Hello There
In the Todo App example, it is not possible neither to add a todo to the todo list nor updating an existing todo without making any additional network calls because updateQueries completely replaces old data.
setState(old => ({
...old,
[queryHash]: {
...old[queryHash],
data,
},
}))
We may request a large amount of data (e.g. to display using a virtualized table) and we may also want the users to be able to edit records or add new ones; in this case, refetchQueries requires fetching a big amount of data just because of partial changes, (the server responds with updated record)
Have any plans on adding any features handling this process?
A temporary solution is to store fetched data in a React state and update it after mutations.
I've written a simple wrapper hook which helps with this situation, I have not tested it though:
import { useEffect, useState } from "react"; import { useQuery as _useQuery } from "@tannerlinsley/react-query-temp"; const useQuery = (...props) => { const [data, setData] = useState(); const {res: data, ...rest} = _useQuery(...props) useEffect(() => { setData(res) }, [res]); return {data, setData, ...rest}; }; export default useQuery;If you expose
getQueryInfo, it would be possible to manipulate thestatedirectly and there will be no need for any extra states (thestatecan be retrieved from_useQueryContextwhich is already exposed).Seems like this is a perfectly simple abstraction for your use case. How would exposing useQueryContext help in this scenario? This is fairly edge case, but possible to solve as you've shown.
_useQueryContextis already exported, It is possible to update the query data directly by manipulating the context if we know thequeryHashof the query we are trying to update.export function _useQueryContext() { return React.useContext(context) }That custom hook would help with this use case but a built-in solution would be better.
The next major version will take care of this. 👍
tannerlinsley closed this
Stay tuned.
Sign in to join this conversation.
Yes that sounds like a very special use case. I probably wouldn’t manage that data in react query.
Tanner Linsley On Oct 24, 2019, 9:14 PM +0200, Amin Zibayi <notifications@github.com>, wrote: