I have unnecessary reference changes, is it a bug? #1493
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
This is not a bug, the normalized-cache will always produce fresh objects, this because it looks at the incoming query and will assemble the result you're asking for in the caching layer. This means that the results aren't referentially equal but they are deeply equal, the properties didn't change, just the object that wraps them. When these results are in-cache the response will be quite instant so multiple queries that are synchronous aren't deduped while in-flight ones are. |
Beta Was this translation helpful? Give feedback.
-
@JoviDeCroock BTW, I'm still unsure how I should use data I fetched using urql But what if it's an array with 50-100 items and I want to trigger This instability kinda makes these sorts of effects very fragile. const [integrationsPairs, setIntegrationsPairs] = useState<IntegrationPair[]>(
[],
)
const [selectedIntegrationId, setSelectedIntegrationId] = useState<string>()
const [result] = useQuery(...)
const { integrations } = result.data ?? {}
useEffect(() => {
if (isNotEmptyOrNullish(codatIntegrations)) {
const integrationPairs = codatIntegrations.map<IntegrationPair>(
(it) => ({
name: it.name,
integrationId: it.id,
}),
)
setIntegrationPairs(integrationPairs)
setSelectedIntegrationId(integrationPairs[0].integrationId)
}
}, [integrations]) Basically when I want to process incoming async data and set it into the local state for some purpose. Surely I can work around above with setting integration id, by just checking whether the current id is not different from new one, but it won't work if I selected another id (in select input), then it will reset again causing me issues again :( Maybe you have some hints on how to handle that? |
Beta Was this translation helpful? Give feedback.
This is not a bug, the normalized-cache will always produce fresh objects, this because it looks at the incoming query and will assemble the result you're asking for in the caching layer. This means that the results aren't referentially equal but they are deeply equal, the properties didn't change, just the object that wraps them.
When these results are in-cache the response will be quite instant so multiple queries that are synchronous aren't deduped while in-flight ones are.