Skip to content

Commit fd591c3

Browse files
committed
fix: resolve hydration mismatch in MusicalNote component
- Initialized currentNote with a fixed value to prevent hydration issues during server-side rendering. - Added client-side logic to set a random note after the component mounts, ensuring consistent behavior across environments.
1 parent 60b422a commit fd591c3

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

apps/frontend/src/modules/shared/components/layout/MusicalNote.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,16 @@ interface MusicalNoteProps {
2626
}
2727

2828
export const MusicalNote = ({ size = 4 }: MusicalNoteProps) => {
29-
const [currentNote, setCurrentNote] = useState(
30-
Math.floor(Math.random() * totalNotes),
31-
);
29+
// Initialize with a fixed value to avoid hydration mismatch
30+
// Then set a random value on the client side
31+
const [currentNote, setCurrentNote] = useState(0);
32+
const [isClient, setIsClient] = useState(false);
33+
34+
// Set random note on client side only to avoid hydration mismatch
35+
useEffect(() => {
36+
setIsClient(true);
37+
setCurrentNote(Math.floor(Math.random() * totalNotes));
38+
}, []);
3239

3340
const noteToCell = useCallback(() => {
3441
const index = Math.abs(currentNote) % totalNotes;

0 commit comments

Comments
 (0)