Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions src/hooks/useCardSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,15 @@ export function useCardSettings(mounted: boolean) {
() => loadCardSettings().options,
);

// Initialize state from storage on mount
useEffect(() => {
if (!mounted || isHydrated) {
return;
}

// Initialize state from storage when mounted
// Setting state during render is the recommended way to adjust state based on props
// and avoid an extra effect cycle (see React docs on "Adjusting some state when a prop changes").
if (mounted && !isHydrated) {
const { layout: storedLayout, options: storedOptions } = loadCardSettings();

// eslint-disable-next-line react-hooks/set-state-in-effect
setLayout((prev) => JSON.stringify(prev) !== JSON.stringify(storedLayout) ? storedLayout : prev);
// eslint-disable-next-line react-hooks/set-state-in-effect
setDisplayOptions((prev) => JSON.stringify(prev) !== JSON.stringify(storedOptions) ? storedOptions : prev);

setLayout(storedLayout);
setDisplayOptions(storedOptions);
setIsHydrated(true);
Comment on lines +25 to 26

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Hydration Replaces Pending Edits

When mounted flips to true, this render path writes the values just read from storage over any queued layout or displayOptions updates that happened before hydration completed. The previous functional setters compared against React's pending state, but these direct setters do not, so an in-flight toggle can be silently discarded before the hydrated render commits.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/hooks/useCardSettings.ts
Line: 25-26

Comment:
**Hydration Replaces Pending Edits**

When `mounted` flips to `true`, this render path writes the values just read from storage over any queued `layout` or `displayOptions` updates that happened before hydration completed. The previous functional setters compared against React's pending state, but these direct setters do not, so an in-flight toggle can be silently discarded before the hydrated render commits.

How can I resolve this? If you propose a fix, please make it concise.

}, [mounted, isHydrated]);
}

// Persist changes to storage
useEffect(() => {
Expand Down
Loading