motion icon indicating copy to clipboard operation
motion copied to clipboard

[BUG] Reorder doesn't work with Next.js

Open cosmoart opened this issue 2 years ago • 21 comments

2. Describe the bug

Reorder.Group and Reorder.Item behave strangely after doing a couple of reorders.

3. IMPORTANT: Provide a CodeSandbox reproduction of the bug

With just React (it works): https://codesandbox.io/p/sandbox/framer-motion-bug-react-c767lp?file=%2Fsrc%2FApp.js With Next.js + React (Not working): https://codesandbox.io/p/devbox/framer-motion-bug-nextjs-lzxrt6?file=%2Fapp%2Fpage.tsx

4. Steps to reproduce

Steps to reproduce the behavior:

  1. Create a project with nextjs npx create-next-app@latest
  2. Install framer motion and paste the example from the documentation
  3. Reorder the numbers and you will see that they stop working after a couple of rearrangements

5. Expected behavior

As in the documentation example

6. Video or screenshots

screen-capture (1).webm

7. Environment details

Framer motion version: 10.12.16 Nextjs version: 13.4.4 React version: 18.2.0

8. Notes

  • The error does not appear on the mobile devices that I have tested

cosmoart avatar Jun 10 '23 22:06 cosmoart

Would also like to +1 on this bug.

I also have a small Github repo which reproduces this bug: https://github.com/jai-x/framer-reorder-bug

Node version: 18.6.0 Framer Motion version: 10.12.16

https://github.com/framer/motion/assets/8362491/be0ad8f3-d95d-4512-affb-84ebf364de64

jai-x avatar Jun 11 '23 12:06 jai-x

Did some version testing: 10.12.16 -> Occurs 10.12.15 -> Occurs 10.12.10 -> Occurs 10.12.9 -> Occurs 10.12.8 -> Occurs 10.12.7 -> Does not occur 10.12.5 -> Does not occur

So that being said rolling back to 10.12.7 fixes that specific issue.

React: 18.2.0 vite: 4.2.0-beta.0

TheAyes avatar Jun 16 '23 07:06 TheAyes

Same issue here. @TheAyes I changed the version in the codesandbox linked to this issue and the bug persists.

"react": "18.2.0",
"next": "^13.4.0",
"framer-motion": "^10.12.16",

mraballard avatar Jun 16 '23 20:06 mraballard

Same issue here. @TheAyes I changed the version in the codesandbox linked to this issue and the bug persists.

"react": "18.2.0",
"next": "^13.4.0",
"framer-motion": "^10.12.16",

That is because Codesandbox doesn't resolve to the older file for some reason.

 "packages": {
    "": {
      "name": "workspace",
      "version": "0.1.0",
      "dependencies": {
       ...
        "framer-motion": "^10.12.7",
        ...
      }
    },
...
"node_modules/framer-motion": {
      "version": "10.12.16",
      "resolved": "https://registry.npmjs.org/framer-motion/-/framer-motion-10.12.16.tgz",
      ...

The Comment I wrote first was testing in my local ide in an environment with just react and framer. So I could easily reinstall the dependencies how much I want. I don't see an option in codesandbox to do that.

TheAyes avatar Jun 17 '23 09:06 TheAyes

Actually. I've made a little oversight. You'll need to remove the little ^ in your package.json and then reinstall dependencies.

TheAyes avatar Jun 17 '23 09:06 TheAyes

Did some version testing: 10.12.16 -> Occurs 10.12.15 -> Occurs 10.12.10 -> Occurs 10.12.9 -> Occurs 10.12.8 -> Occurs 10.12.7 -> Does not occur 10.12.5 -> Does not occur

So that being said rolling back to 10.12.7 fixes that specific issue.

React: 18.2.0 vite: 4.2.0-beta.0

@TheAyes I just changed the package version to "^10.12.6" and I can confirm that it works properly.

codeyacine avatar Jun 19 '23 09:06 codeyacine

It works normally when I change the version to 10.12.7

forward-step avatar Jun 24 '23 03:06 forward-step

@forward-step I can confirm that

andreyvital avatar Jul 10 '23 23:07 andreyvital

It's broken for me and I can confirm @forward-step solution of downgrading works.

maderesponsively avatar Jul 19 '23 10:07 maderesponsively

@maderesponsively @forward-step same here, without Next, just [email protected]

elunomas avatar Jul 26 '23 14:07 elunomas

Had to downgrade to 10.12.7 for it to work with next

jacksmethurst avatar Aug 09 '23 12:08 jacksmethurst

I can reproduce the same error with Vite on versions > 10.12.7. So it is not specific to NextJS.

I am on latest (v10.16.1) and the problem still persists.

robertotcestari avatar Aug 23 '23 10:08 robertotcestari

Any real solution so far ?

I found out that each time you start to drag an element, the array containing all items is updated (via the callback function passed to onReorder) everytime the dragged element pass over another element. So if i have an array [1, 2, 3, 4] and i want to drag "1" to the end, the array will be updated 4 times : [2,1,3,4] then [2,3,1,4] then [2,3,4,1].

The problem i think is that sometimes, that update occur too early and too many times unecessarly. The animation sometimes is not completed and the drag is not completed yet (mouse still down). Sometimes, the array is already updated and the animation is not completed yet causing the element to be not draggable anymore or causing random behaviour.

I don't know, it's my own analyze. But i think it would be great if the array is update only and only when the drag action is complete (when the mouse is finally up) and the animation is done.

ARTony8 avatar Sep 05 '23 21:09 ARTony8

Confirming the issue on React 18.2.0, Vite 4.4.5 with Framer Motion 10.16.4...

b8zeek avatar Nov 05 '23 02:11 b8zeek

Got same buggy behavior with reordering, works fine on 10.12.7

nodegin avatar Nov 07 '23 17:11 nodegin

Yeah, version 10.12.7 works for me, too.

b8zeek avatar Nov 07 '23 17:11 b8zeek

Same here, but how can such a problem persist through so many versions ...

tkoehlerlg avatar Nov 26 '23 10:11 tkoehlerlg

Ran into this as well but was seems I found a way to get the intended result. Wrapping in a transition fixed it for me.

  const [_, startTransition] = useTransition();
  const [items, setItems] = useState([1,2,3,4);

  function onReorder(array: any[]): void {
    startTransition(() => {
      setItems(array);
    });
  }

TheJoeCollins avatar Mar 12 '24 18:03 TheJoeCollins

Is this issue still unresolved? Some comments suggest that a simple version change might resolve it.

Niharika0104 avatar Aug 10 '24 09:08 Niharika0104