issues with dnd-kit using React Query (fetched list jumps each time a list item is reordered) (trpc)
if anyone has dnd-kit (https://docs.dndkit.com/) sortable preset working with an app fetching from react-query, i'd love to take a look. I'm having all sorts of issues with rendering when re-ordering my list. if needed, i can screen grab the behavior as well. it's almost like its refetching when i'm setting the state, or something along these lines, and it briefly renders the old list before displaying the updated one. any help is
the gist of the code is
the gist of the code is
11 Replies
xenial-black•3y ago
Can you show codesandbox reproduction?
other-emeraldOP•3y ago
I can try and reproduce it tomorrow on CodeSandbox
here is the codesandbox https://codesandbox.io/p/github/jackbisceglia/p4p/draft/elastic-hoover?workspaceId=6e73eef8-1ef6-4cf6-879b-1bda62070abd&file=%2Fsrc%2Fserver%2Fapi%2Frouters%2Fselection.ts&selection=%5B%7B%22endColumn%22%3A6%2C%22endLineNumber%22%3A255%2C%22startColumn%22%3A6%2C%22startLineNumber%22%3A255%7D%5D&workspace=%257B%2522activeFileId%2522%253A%2522cle7ike3h001qg6h6h9700z2d%2522%252C%2522openFiles%2522%253A%255B%2522%252FREADME.md%2522%252C%2522%252Fsrc%252Fenv%252Fschema.mjs%2522%252C%2522%252Fsrc%252Fenv.mjs%2522%252C%2522%252Fsrc%252Fpages%252Fme.tsx%2522%252C%2522%252Fsrc%252Fserver%252Fapi%252Frouters%252Fselection.ts%2522%255D%252C%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522gitSidebarPanel%2522%253A%2522COMMIT%2522%252C%2522spaces%2522%253A%257B%2522cle7ikfza0015356iqszxfz29%2522%253A%257B%2522key%2522%253A%2522cle7ikfza0015356iqszxfz29%2522%252C%2522name%2522%253A%2522Default%2522%252C%2522devtools%2522%253A%255B%257B%2522type%2522%253A%2522PREVIEW%2522%252C%2522taskId%2522%253A%2522dev%2522%252C%2522port%2522%253A3000%252C%2522key%2522%253A%2522cle7ilui8010e356ijv3hluyd%2522%252C%2522isMinimized%2522%253Afalse%252C%2522path%2522%253A%2522%252Fsocial%2522%257D%252C%257B%2522type%2522%253A%2522TASK_LOG%2522%252C%2522taskId%2522%253A%2522dev%2522%252C%2522key%2522%253A%2522cle7il11y007h356i0kommtw7%2522%252C%2522isMinimized%2522%253Afalse%257D%255D%257D%257D%252C%2522currentSpace%2522%253A%2522cle7ikfza0015356iqszxfz29%2522%252C%2522spacesOrder%2522%253A%255B%2522cle7ikfza0015356iqszxfz29%2522%255D%252C%2522hideCodeEditor%2522%253Afalse%257D
i had to make a lot of the db stuff just return mock data, but the issue is still happening so i think its illustrates it properly
not sure why the link is so large
most of the code lives in the <ListEditor /> in
me.tsx, and then the <SortableWrapper /> in components/cards/Editable.tsxxenial-black•3y ago
getting this:
other-emeraldOP•3y ago
Ah maybe my changes didn’t persist in the link I sent. One moment
https://codesandbox.io/p/github/jackbisceglia/p4p/code-sandbox-reproduction?workspaceId=6e73eef8-1ef6-4cf6-879b-1bda62070abd&file=%2Fsrc%2Fserver%2Fapi%2Frouters%2Fselection.ts&selection=%5B%7B%22endColumn%22%3A16%2C%22endLineNumber%22%3A61%2C%22startColumn%22%3A16%2C%22startLineNumber%22%3A61%7D%5D&workspace=%257B%2522activeFileId%2522%253A%2522cle7ike3h001qg6h6h9700z2d%2522%252C%2522openFiles%2522%253A%255B%2522%252FREADME.md%2522%252C%2522%252Fsrc%252Fenv%252Fschema.mjs%2522%252C%2522%252Fsrc%252Fenv.mjs%2522%252C%2522%252Fsrc%252Fpages%252Fme.tsx%2522%252C%2522%252Fsrc%252Fserver%252Fapi%252Frouters%252Fselection.ts%2522%255D%252C%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522gitSidebarPanel%2522%253A%2522COMMIT%2522%252C%2522spaces%2522%253A%257B%2522cle7ikfza0015356iqszxfz29%2522%253A%257B%2522key%2522%253A%2522cle7ikfza0015356iqszxfz29%2522%252C%2522name%2522%253A%2522Default%2522%252C%2522devtools%2522%253A%255B%257B%2522type%2522%253A%2522PREVIEW%2522%252C%2522taskId%2522%253A%2522dev%2522%252C%2522port%2522%253A3000%252C%2522key%2522%253A%2522cle7ilui8010e356ijv3hluyd%2522%252C%2522isMinimized%2522%253Afalse%252C%2522path%2522%253A%2522%252Fsocial%2522%257D%252C%257B%2522type%2522%253A%2522TASK_LOG%2522%252C%2522taskId%2522%253A%2522dev%2522%252C%2522key%2522%253A%2522cle7il11y007h356i0kommtw7%2522%252C%2522isMinimized%2522%253Afalse%257D%255D%257D%257D%252C%2522currentSpace%2522%253A%2522cle7ikfza0015356iqszxfz29%2522%252C%2522spacesOrder%2522%253A%255B%2522cle7ikfza0015356iqszxfz29%2522%255D%252C%2522hideCodeEditor%2522%253Afalse%257D
does this work? it should open to a branch
code-sandbox-reproduction-- there shouldn't be any env variables being parsed on this version
additionally, /me is the url to examine
Oh ok figured it out, you just have to switch to that branch “code-sandbox-reproduction”. Sorry I’ve never used code sandbox beforexenial-black•3y ago
still fails at installing dependencies for me. I don't understand why you need such a complex setup? Wouldn't a much smaller reproduction suffice to show the issue?
other-emeraldOP•3y ago
hmm thats super strange; yea ill reproduce it and then send the updated one
https://codesandbox.io/p/github/jackbisceglia/dndkit-reactquery/main?workspaceId=6e73eef8-1ef6-4cf6-879b-1bda62070abd&file=%2FREADME.md&workspace=%257B%2522activeFileId%2522%253A%2522cleag8jz2000ng1hx8ece6p7g%2522%252C%2522openFiles%2522%253A%255B%2522%252FREADME.md%2522%255D%252C%2522sidebarPanel%2522%253A%2522EXPLORER%2522%252C%2522gitSidebarPanel%2522%253A%2522COMMIT%2522%252C%2522spaces%2522%253A%257B%2522cleag8ka1001d356iu9u5mrf1%2522%253A%257B%2522key%2522%253A%2522cleag8ka1001d356iu9u5mrf1%2522%252C%2522name%2522%253A%2522Default%2522%252C%2522devtools%2522%253A%255B%257B%2522type%2522%253A%2522PREVIEW%2522%252C%2522taskId%2522%253A%2522dev%2522%252C%2522port%2522%253A3000%252C%2522key%2522%253A%2522cleag8yg5009k356imo5qtv0p%2522%252C%2522isMinimized%2522%253Afalse%257D%252C%257B%2522type%2522%253A%2522TASK_LOG%2522%252C%2522taskId%2522%253A%2522dev%2522%252C%2522key%2522%253A%2522cleag8wmh005p356itcqve1lw%2522%252C%2522isMinimized%2522%253Afalse%257D%255D%257D%257D%252C%2522currentSpace%2522%253A%2522cleag8ka1001d356iu9u5mrf1%2522%252C%2522spacesOrder%2522%253A%255B%2522cleag8ka1001d356iu9u5mrf1%2522%255D%252C%2522hideCodeEditor%2522%253Afalse%257D
here's the link; items have to be dragged by the text. you should see a jumping glitch when reordering
xenial-black•3y ago
seems to work fine if you remove the
transition: https://codesandbox.io/p/github/jackbisceglia/dndkit-reactquery/csb-hbnx6o/draft/focused-fermat?file=%2Fsrc%2Fcomponents%2FSortableItem.tsx&selection=%5B%7B%22endColumn%22%3A5%2C%22endLineNumber%22%3A17%2C%22startColumn%22%3A5%2C%22startLineNumber%22%3A17%7D%5Ddndkit-reactquery/csb-hbnx6o/draft/focused-fermat - CodeSandbox
CodeSandbox is an online editor tailored for web applications.
xenial-black•3y ago
not sure how this is related with react-query - I guess you would have the same issue without it ...
other-emeraldOP•3y ago
i guess i didnt sufficiently reproduce it as this doesnt fix my actual issue. the reason i think its react-query/trpc related is because when i instead serve the same data from an express server and fetch/store state with react hooks, i have no issue
https://codesandbox.io/p/github/jackbisceglia/dndkit-reactquery/csb-hbnx6o/draft/focused-fermat?file=%2Fsrc%2Fpages%2Findex.tsx&selection=%5B%7B%22endColumn%22%3A48%2C%22endLineNumber%22%3A103%2C%22startColumn%22%3A48%2C%22startLineNumber%22%3A103%7D%5D here it's working fine when just storing in state, with transition applied.
interestingly enough, removing the key from the mapping also fixes this issue. i was thinking maybe it has something to do with react query's cache/refetching, but thats just a guess. it just seems weird to me that i don't get this issue using other fetching techniques or when i dont pass the id as a key from the mapped over obejcts
xenial-black•3y ago
So this likely means the key is changing, which leads to react re-mounting the node, thus re-animating it... can you log the keys to see the differences?
other-emeraldOP•2y ago
Ok yea I’ll check it out
Ok so this is random since it's like 15 months later lol, but I rebuilt this from scratch again with same stack (trpc/react query/dnd-kit) and am facing the same bug.
tl;dr is that i get an animation error when i reorder an array mapped from data fetched by react-query. however, when instead I instead pass the data from react query to a useState hook and map over that, I don't get any animation error. just seems really weird to me
i figured i might as well try to wrap the call that updates the trpc data in
Sorry nevermind on most of that ^, it's what I get for trying to tackle this at 2am. I guess my question is ultimately, is there any downsides to feeding the result of a react query fetch into useState for it's initial data? The flow I'm using (that somehow works) is:
fetch from react query
--> pass data from fetch to state hook
--> (at some point) update react query cache
--> forces rerender
--> my state hook is given the new data from the cache
and this some way or another gets around key issues in the .map
useTransition to mitigate this, and for some reason this fixes the bug. I honestly have a really weak understanding of useTransition and when to use it. I guess I'm just wondering if it makes any sense that this would solve the issue? edit: it doesn't quite fix it entirely, but my question still remains