blog/breaking-react-querys-api-on-purpose #94
Replies: 39 comments 76 replies
-
So, with the |
Beta Was this translation helpful? Give feedback.
-
Last time, I had similar need and try onSucces with useQuery but as you said, it was not what I need :) My issue was I need to use query data with additional fields that I should be able to update. It was like articles that I'm showing them on home screen with cards. However I need some additional fields like isActive and order. Also I'm hydrating state on serverside so there is no loading etc. So what is the best way doing that and how would you approach it? Thanks for great article. |
Beta Was this translation helpful? Give feedback.
-
I felt exactly in the onSuccess > dispatch issue. We are still juggling with redux on our project and I didn’t get why my action was triggered 20 times 😅 |
Beta Was this translation helpful? Give feedback.
-
Please provide a good migration path 🙏 Mark the APIs as deprecated or give us linting rules so we can address the breakage in our large project before without needing to do it all at once when upgrading. |
Beta Was this translation helpful? Give feedback.
-
Is there a way to use |
Beta Was this translation helpful? Give feedback.
-
The current use case I have for onSuccess, is invalidating other queries in the cache. For example, I know in my code any time the data is fetched from a certain set of useQuery hooks, i call invalidateQuery() on a particular useQuery instance. It's not a mutation that happens, just any time I fetch certain data, I need to make sure I refetch another useQuery and have the freshest instance of it. Going through the article, I haven't found a good replacement option yet. How might one handle such a use case, would you be able to provide a suggestion please? |
Beta Was this translation helpful? Give feedback.
-
I found one example when I used the function trackSearch(text: string, totalCount: number) {
track('eventName', text, totalCount)
} And I need somehow detect that the query function triggers by |
Beta Was this translation helpful? Give feedback.
-
I've reposted this from the RFC: TanStack/query#5279 (comment) @TkDodo So currently we use the callbacks to update query parameters after making sure that it won't create a rendering loop.
We also use it to update our redux store when certain data is required by client side state or thunks For example organisation data that we need in redux state
Do you think there are better ways to do these? The useEffect loop ads more render cycles and dependency gotcha's to this whole process. How else would you recommend to do this? |
Beta Was this translation helpful? Give feedback.
-
So if I need the data from the query in order to perform other actions such as a mutation, the best way to do it is with an I saw your response in a comment from nkeyy0 where you advice to do a side action in the fetch function. So could something like this work? Or even be good practice?
|
Beta Was this translation helpful? Give feedback.
-
I have a use-case where the user can optionally fill a form using external data (fetched from an external rest api). So they click a button which sets a value using the first form field, thus enabling the query. If the query is successful I set the form fields accordingly.
So replace this with a |
Beta Was this translation helpful? Give feedback.
-
I am using useQuery with wrapper like
In this case, we don't have severals callback in other component. Global queryClient is hard to handle mutilple error case |
Beta Was this translation helpful? Give feedback.
-
What's the recommended approach for populating an editable form? Should I set separate state via useEffect by watching the query's data? Something like this?
|
Beta Was this translation helpful? Give feedback.
-
I find myself quite tempted to try and sync state with I'm building a chatbot feature in my application and the flow looks like so:
Does it even make sense to use RQ for this? I don't need any caching but I'd like to use |
Beta Was this translation helpful? Give feedback.
-
Great blog as usual @TkDodo 🎉. I just wanted to suggest one thing, for the export function useTodos(filters) {
const { dispatch } = useDispatch()
const query = useQuery({
queryKey: ['todos', 'list', { filters }],
queryFn: () => fetchTodos(filters),
staleTime: 2 * 60 * 1000,
})
const [prevData, setPrevData] = useState(query.data)
if (query.data !== prevData) {
setPrevData(data)
dispatch(setTodos(query.data))
}
return query
} Updating state during rendering might look like an anti-pattern, and even I used think that
And docs also mentioned the things you already mentioned which is state syncing is bad anyways and should be avoided unless absolutely necessary. |
Beta Was this translation helpful? Give feedback.
-
We were using |
Beta Was this translation helpful? Give feedback.
-
Hi TkDodo, E.G:
My issue is that the useEffect runs on initial render which I do not want, which is why it would be better to have the |
Beta Was this translation helpful? Give feedback.
-
Is there a reason something like the following would not work? I notice none of the examples use console logs on error. We wish to log the errors a certain way. I'm debating if this is an issue with our setup or console logs should not be used in queryCache this way. queryCache: new QueryCache({
onError: (error, query) => {
console.log("file: QueryClientConfig.tsx:52 error ~ error:", error);
},
onSuccess: (data, query) => {
console.log("file: QueryClientConfig.tsx:52 success ~ data:", data);
},
}), |
Beta Was this translation helpful? Give feedback.
-
Hi @TkDodo
|
Beta Was this translation helpful? Give feedback.
-
Using trpc with onSuccess, works great if you know what you are doing. You can also refetch. |
Beta Was this translation helpful? Give feedback.
-
const { data, isLoading, isFetching } = useQuery<GetChargerDetailsSuccessType, unknown, GetChargerDetailsSuccessType>({ I am using react-hook-form to get the details from an API and populate an edit form. For that I used the setValue of react-hook-form in the onSuccess callback. What should be the approach now in v5? |
Beta Was this translation helpful? Give feedback.
-
Nice explanation and It really make sense. I've read it and i understand why you did it. Thank you for be a excellent developer. |
Beta Was this translation helpful? Give feedback.
-
I'm currently facing an issue with my implementation using React Query where unnecessary API requests are being triggered due to the way dependencies are set up in my queries. Here's the situation: When the Here's a summary of the problem: I want to prevent the first unnecessary request made by Below are the relevant parts of the code for context:
For a more complete view of the code and its context, please find it here. The core of the issue lies in managing dependencies and triggering |
Beta Was this translation helpful? Give feedback.
-
Small question, i am using isError in my component, once the isError is true even if the component unmounts and mounts again, the state of isError remains true and the toast message is reappearing. Any idea how to go about such a situation ? |
Beta Was this translation helpful? Give feedback.
-
Hi all! I know I might be late, but I would really appreciate a piece of advice for my use case which seems to be quite simple and common, but difficult to solve without Let's say I have a dialog with several sections and "Submit" button at the bottom. Each section is represented by a child component, e.g. component In my How can I pass the eventual selection from the child component to the parent? An obvious idea is to have Sorry if I've missed or misunderstood something. I've read through most of comments here and different approaches suggested in the "Practical React Query" blog, but still cannot find an elegant solution. Thanks! |
Beta Was this translation helpful? Give feedback.
-
Thanks for another amazing post TkDodo! 😁 I'm just a little bit worried about the
Thanks! |
Beta Was this translation helpful? Give feedback.
-
What should i do if i'm using Zustand to store the data, i was using the onSuccess to save this data in Zustand, i realized that it's not good, but it was someone more experienced than me who told me to use onSuccess, but now i'm having a lot of rerendering cycle because of this, does anyone know how can i save the data in the zustand store without onSuccess? |
Beta Was this translation helpful? Give feedback.
-
How to use error |
Beta Was this translation helpful? Give feedback.
-
I’m using dispatch to show my error message, how can I use dispatch in my global onError function when my useDispatch should be in component function?
|
Beta Was this translation helpful? Give feedback.
-
Is onSuccess being deprecated from useInfiniteQuery as well? I have a scenario where I need to dispatch an action after receiving data. What is the recommended approach if onSuccess is deprecated? |
Beta Was this translation helpful? Give feedback.
-
I fully understand the motivation behind this. I am wondering what would be a go to way of handling things when data returned from query needs to be used to do some imperative operations (e.g. create elements on a "board"). This is a case for me as as the "board" component is not capable of working in a declarative manner due to performance issues. Is there any other way to handle such use cases other than using custom useEffect? If not what actually should be placed in the dependency array of such useEffect - query.data, query.isSuccess or maybe even something else? |
Beta Was this translation helpful? Give feedback.
-
blog/breaking-react-querys-api-on-purpose
Why good API design matters, even if it means breaking existing APIs in the face of resistance.
https://tkdodo.eu/blog/breaking-react-querys-api-on-purpose
Beta Was this translation helpful? Give feedback.
All reactions