You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I spent a while debugging this. Basically the problem is that when I selected some data from a shape in one route of my app e.g. /page/1, the selected data is what my selector saw when I went to /page/2. Since I was filtering the array of rows down to a single row, this broke my selector.
I'm building my app in Remix which re-uses the same component when switching routes on the same route component — so the same hook instance of useShape is used.
To mitigate, you can for now just clone the object passed to the selector and mutate that. Avoid directly mutating the shape data. In the future we could two things to make this simpler to manage.
Don't use useRef.
Instead use per shape instance cache. I prototyped something like this:
…ges (#1615)
Addresses #1446
There is some weirdness with the `useSyncExternalStoreWithSelector` hook
when passed a ref w.r.t. to selector changing - need to look into this
deeper, I suspect memoization is using referential equality checks which
is messing things up.
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.
# Releases
## @electric-sql/[email protected]
### Patch Changes
- 3c8f662: Fix `useShape` not returning correct data upon changing
`selector` prop - see
<#1446>.
- b0f39c7: add test that useShape re-renders on where state change
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
I spent a while debugging this. Basically the problem is that when I selected some data from a shape in one route of my app e.g.
/page/1
, the selected data is what my selector saw when I went to/page/2
. Since I was filtering the array of rows down to a single row, this broke my selector.The problem is two-fold.
useRef
https://github.com/electric-sql/electric-next/blob/340596deba607494251db565be82141a5782ff22/packages/react-hooks/src/react-hooks.tsx#L150 — which caches data across all instances of a hook as I understand it.To mitigate, you can for now just clone the object passed to the selector and mutate that. Avoid directly mutating the shape data. In the future we could two things to make this simpler to manage.
useRef
.Instead use per shape instance cache. I prototyped something like this:
Second we could allow a dependencies array which resets the shapeData whenever it changes so e.g. in my case you could call:
The text was updated successfully, but these errors were encountered: