fix: sidebar skeleton hydration mismatch #8849
Open
+4
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes a hydration mismatch error caused by using
Math.random()insideuseMemo(oruseStateinitializers) in theSidebarMenuSkeletoncomponent.Supersedes #8510
This PR supersedes #8510.
While the approach in #8510 resolves the linter error, it uses
useStatewith a random initializer. In Next.js (SSR), this causes a Hydration Mismatch because the server generates one random number and the client generates a different one during the initial render.The Solution
I implemented a two-pass approach to ensure hydration safety:
"50%") so the Server HTML and Client HTML are identical.useEffecthook randomizes the width immediately after mount.This prevents the "Text content does not match server-rendered HTML" warning and ensures the component is theoretically pure during the render phase.
Type of change
Checklist: