简体   繁体   English

NextJs / Framer Motion:使用 useViewportScroll 时我收到此警告:警告:useLayoutEffect 在服务器上不执行任何操作,

[英]NextJs / Framer Motion: When using useViewportScroll I got this warning: Warning: useLayoutEffect does nothing on the server,

I am using NextJs with Framer Motion.我将 NextJs 与 Framer Motion 一起使用。 Everything is working properly but I got this warning:一切正常,但我收到此警告:

Warning: useLayoutEffect does nothing on the server, because its effect cannot be encoded into the server renderer's output format. This will lead to a mismatch between the initial, non-hydrated UI and the intended UI. To avoid this, useLayoutEffect should only be used in components that render exclusively on the client.

Seems like this will be get fixed in the upcoming framer-motion version.似乎这将在即将推出的 framer-motion 版本中得到修复。

From the GitHub conversation来自 GitHub 的对话

The React 18 upgrade in #1438 introduced a new useIsMounted hook, that that didn't use the useIsomorphicLayoutEffect. #1438 中的 React 18 升级引入了一个新的 useIsMounted 挂钩,它没有使用 useIsomorphicLayoutEffect。 This resulted in a warning rendered serverside.这导致服务器端出现警告。 Fix by replacing useLayoutEffect with useIsomprohicLayoutEffect通过将 useLayoutEffect 替换为 useIsomprohicLayoutEffect 进行修复

https://github.com/framer/motion/pull/1452 https://github.com/framer/motion/pull/1452

For a temporary fix, you can cancel rendering the the component if it is not client render, by adding such as对于临时修复,如果组件不是客户端渲染,您可以取消渲染组件,方法是添加诸如

const [isLoaded, setLoaded] = useState(false);
useEffect(() => {
  setLoaded(true);
}, []);

if (!isLoaded) {
  return null;
}

return (
  ...your component
);

Next does SSR which is why its throwing this warning as useLayoutEffect doesn't run on server.接下来是SSR ,这就是为什么它抛出这个警告,因为 useLayoutEffect 不在服务器上运行。

Official docs of useLayoutEffect : useLayoutEffect 的官方文档

The signature is identical to useEffect, but it fires synchronously after all DOM mutations.签名与 useEffect 相同,但它在所有 DOM 突变后同步触发。 Use this to read layout from the DOM and synchronously re-render.使用它从 DOM 中读取布局并同步重新渲染。 Updates scheduled inside useLayoutEffect will be flushed synchronously, before the browser has a chance to paint.在 useLayoutEffect 中安排的更新将在浏览器有机会绘制之前同步刷新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复“警告:useLayoutEffect 在服务器上什么都不做”? - How to fix the "Warning: useLayoutEffect does nothing on the server"? 如何在使用 Material UI 和 reactDOMServer 时修复“警告:useLayoutEffect 在服务器上什么都不做”? - How to fix "Warning: useLayoutEffect does nothing on the server" while using Material UI and reactDOMServer? usePreventScroll 在 Nextjs 中导致 useLayoutEffect 警告 - usePreventScroll causes useLayoutEffect warning in Nextjs framer-motion layoutTransition 在 React 中有效,但在 NextJS 中无效 - framer-motion layoutTransition does work in React, but not in NextJS 带有 Framer Motion 和 NextJS 的出口 animation 的布局移位 - Layout shift on exit animation with Framer Motion and NextJS Framer Motion 退出和 AnimatePresence 在 Nextjs 中不起作用? - Framer Motion exit and AnimatePresence not working in Nextjs? Next JS - 使用 framer-motion 时出现语法错误 - Next JS - SyntaxError when using framer-motion 使用 AnimatePresence 和 React HOC 时成帧器运动不呈现退出动画 - Framer motion not rendering exit animation when using AnimatePresence and React HOC 如何使用成帧器运动在反应组件之间切换? - How can i switch between react components using framer motion? 我可以使用成帧器运动创建一个 blob animation 吗? - Can i create a blob animation using framer motion?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM