react-spring icon indicating copy to clipboard operation
react-spring copied to clipboard

[bug]: useSpring Events not firing (Next.js 13)

Open lairdkruger opened this issue 2 years ago • 13 comments

Which react-spring target are you using?

  • [X] @react-spring/web
  • [ ] @react-spring/three
  • [ ] @react-spring/native
  • [ ] @react-spring/konva
  • [ ] @react-spring/zdog

What version of react-spring are you using?

9.7.2

What's Wrong?

API events are not triggering at all.

To Reproduce

Setup a Next.js 13.3.0 project, try useSpring with events. Try to do something in the event eg: const [{ x }, api] = useSpring( () => ({ x: 0, // onChange is currently broken with Next App structure onChange: { x: () => { console.log("onChange"); // This should be logging the value on drag }, }, }), [] );

Expected Behaviour

Expected console to log "onChange".

Link to repo

https://codesandbox.io/p/sandbox/dawn-leaf-lqof4b?selection=%5B%7B%22endColumn%22%3A11%2C%22endLineNumber%22%3A12%2C%22startColumn%22%3A11%2C%22startLineNumber%22%3A12%7D%5D&file=%2Fpages%2Fworking%2Findex.tsx

lairdkruger avatar Apr 14 '23 00:04 lairdkruger

I'm also encountering this bug, with @react-spring/[email protected] and [email protected], using the app router as in the above repo. The springs work (they might not be recognizing config, I'm very inexperienced with the library and can't tell). Events like onStart do not fire.

bramarcade avatar May 10 '23 13:05 bramarcade

Yup, onStart, onRest do not work, as well as things like api.start or api.stop. I am using Next 13.4.19, and react-spring 9.7.2

ionutpopa avatar Sep 28 '23 09:09 ionutpopa

I found that something happens with the React life-cycle within useSprings. There is useIsomorphicLayoutEffect, that should run with updates array, that consist of config, callback and other props. Updates is simple array variable (not react ref) and it rewrites on every render, but it calculates in useMemo on first render. So, I debugged on some examples on react-spring site and found that useIsomorphicLayoutEffect should be called with not empty updates array, but in Next.JS 13 effect calls with empty array

skibitskiy avatar Oct 01 '23 12:10 skibitskiy

I made hack to fix it in Next. You can edit sources of useSprings. Instead of const updates = [] here write this code:

const updatesRef = useRef([])
const { current: updates } = updatesRef;

@ionutpopa fyi

skibitskiy avatar Oct 01 '23 12:10 skibitskiy

Also I noted that app renders twice and it causes current problem. There is the easier way to make react-spring work – turn off the React strict mode (for React 18)

https://stackoverflow.com/questions/71847778/why-my-nextjs-component-is-rendering-twice

const nextConfig = {
  reactStrictMode: false
}

module.exports = nextConfig

skibitskiy avatar Oct 02 '23 18:10 skibitskiy

@skibitskiy just want to say a big thank you for finding the fix. spent about a day trying to debug myself and work out what was wrong. setting reactStrictMode: false worked a treat!

I'm guess it's a big with nextJS rather than react-spring

fortydegrees avatar Oct 13 '23 10:10 fortydegrees

@skibitskiy huge thanks from me as well. I upgraded my app to Next14 and have been turning my wheels trying to figure out what change broke my animations.

WestonVincze avatar Nov 08 '23 02:11 WestonVincze

This also helped me with animations using useTrail not getting trigger at first render.

tovimx avatar Dec 07 '23 05:12 tovimx

I am also experiencing this bug during development.

I am using Spring to animate bars in a graph, on entry and on data change. However, because of React Strict mode, it is rendering twice in quick succession which is causing the bars to get stuck on their inital "to" configuration, and fail to animate to their provided "from" config in the UseSpring hook.

Turning off strict mode does fix the problem, but unfortunatly will turn off strict mode app wide, which can be a useful feature to keep. It goes without saying, that this does not effect production outputs then.

JavanFriedel avatar Jan 18 '24 03:01 JavanFriedel

I was migrating an app that used pages to app directory (Nextjs v14) and all the animations with react-spring stopped working.

When I changed reactStrictMode to false, all the animations started working again magically. Thanks @skibitskiy , you saved the day 🚀

willydavid1 avatar Feb 10 '24 22:02 willydavid1

@skibitskiy 谢谢!

yymm120 avatar Mar 23 '24 17:03 yymm120