Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback after success and data changed

Tags:

react-query

I used the onSuccess callback, but it is called every time the data is successfully retrieved.

I expect to be called only when the data changed(deep equal). Is there any good way to do this?

useQuery(..., {
  onSuccess(data) {
    console.log(data)
  }
})

// first: [1], second: [2], // => call
// first: [1], second: [1], // => not call

How to do this, thanks a lot!

like image 344
Mxcpanel Avatar asked Oct 31 '25 22:10

Mxcpanel


1 Answers

onSuccess is tied to successful data fetching. If you want to spawn a side-effect whenever data changes, use a useEffect:

const { data } = useQuery(...)
useEffect(() => ..., [data])
like image 55
TkDodo Avatar answered Nov 03 '25 20:11

TkDodo