I believe it's because Retool is a big React app and that's how setState
in React works.
setState()
does not always immediately update the component. It may batch or defer the update until later. This makes readingthis.state
right after callingsetState()
a potential pitfall.
It's the same reason that setting a variable and immediately reading from it without checking Keep variable references inside the query in sync with your app
doesn't work. As your Javascript runs it doesn't defer to React state updates by default and so calling query.data
will just fetch whatever version of that object exists in memory while the state setter that works underneath the hood may or may not be done mutating it.
Assigning the query result to a variable as shown above is going to be the quickest and simplest way of immediately using query data.