fix(react): resolve theme toggle hydration mismatch - #1103
Open
puneetnith28 wants to merge 1 commit into
Open
puneetnith28 wants to merge 1 commit into
puneetnith28 wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Author
|
Please review this PR and let me know if any further changes are needed. |
Comment on lines
+34
to
+39
| useEffect(() => { | ||
| const stored = localStorage.getItem('theme'); | ||
| if (stored) { | ||
| setPref(stored); | ||
| } | ||
| }, []); |
Contributor
There was a problem hiding this comment.
Suggested change
| useEffect(() => { | |
| const stored = localStorage.getItem('theme'); | |
| if (stored) { | |
| setPref(stored); | |
| } | |
| }, []); | |
| useEffect(() => { | |
| const stored = localStorage.getItem('theme'); | |
| if (stored === 'light' || stored === 'dark') { | |
| setPref(stored); | |
| } | |
| }, []); |
Contributor
There was a problem hiding this comment.
we dont need to reapply system
Member
There was a problem hiding this comment.
wouldn’t stored !== pref be a little more robust?
Contributor
There was a problem hiding this comment.
if somehow stored is returned as null, its better to check explicitly. WDYT?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Fixes a hydration mismatch where the
ThemeToggleicon resets to thesystemicon after page reload when a user preference (dark/light) is stored.Changes
prefstate to'system'by default inuseTheme.mjsto ensure server HTML and client initial render match.prefwithlocalStorageinside auseEffecthook on client mount.key={themePreference}to<ThemeToggle>to ensure clean remounting when the theme preference is applied.References