Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] - REDUCE MOTION WITH CALENDAR HYDRATATION #3395

Open
viclafouch opened this issue Jul 2, 2024 · 2 comments
Open

[BUG] - REDUCE MOTION WITH CALENDAR HYDRATATION #3395

viclafouch opened this issue Jul 2, 2024 · 2 comments
Assignees

Comments

@viclafouch
Copy link

viclafouch commented Jul 2, 2024

NextUI Version

2.4.2

Describe the bug

The component Calendar has an hydratation issue with disableAnimation prop (from NextUIProvider) when using SSR and useReduceMotion.

Let's say we want to disable animation when user has reduce motion enabled.

On server, disableAnimation is undefined because we don't know the config of reduce motion from server (that's normal).
On client, if disableAnimation is true, we have hydratation issue.

Steps to Reproduce the Bug or Issue

  1. Enable reduceMotion on your machine
  2. Add this in your app.tsx
 const isReducedMotion = useReducedMotion() // from framer-motion

// ....

// HYDRATATION ISSUE HERE
<NextUIProvider disableAnimation={isReducedMotion}>
   <Calendar calendarWidth={350} />
</NextUIProvider>

Expected behavior

No Hydration issue just because of reduceMotion is changing to true to false or whatever

Screenshots or Videos

Capture d’écran 2024-07-02 à 15 59 51

Operating System Version

MacOS

Browser

Chrome

Copy link

linear bot commented Jul 2, 2024

@jrgarciadev jrgarciadev added the Improvement label Jul 8, 2024 — with Linear
@ryo-manba ryo-manba self-assigned this Jul 9, 2024
@ryo-manba
Copy link
Member

To avoid the hydration error, it's necessary to ensure that the initial render is consistent between the server and the client.
useEffect hook ensures that the state is only updated on the client side, avoiding discrepancies between the server-rendering and client-rendering HTML.

The following code resolves the issue.

export default function Page() {
  const reducedMotion = useReducedMotion();
  const [isReducedMotion, setIsReducedMotion] = useState(false);

  useEffect(() => {
    setIsReducedMotion(reducedMotion ?? false);
  }, [reducedMotion]);

  return (
    <NextUIProvider disableAnimation={isReducedMotion}>
      <Calendar calendarWidth={350} />
    </NextUIProvider>
  );
}

Reference: vercel/next.js#17443 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants