Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd committed Dec 22, 2023
1 parent 5e1f3bd commit 4c7ac30
Showing 1 changed file with 3 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,17 @@ function useSyncedState<T>(initialValue: T | undefined) {
const [state, setState] = useState<T | undefined>(initialValue);
const ref = useRef<T | undefined>(initialValue);

// Synchronize ref with state
// Synchronize ref with state.
useEffect(() => {
ref.current = state;
}, [state]);

// Function to update both state and ref
// Function to update both state and ref.
const setSyncedState = useCallback((newValue: T | undefined) => {
setState(newValue);
ref.current = newValue; // This line isn't strictly necessary because of the useEffect above
}, []);

// Return both state and ref, along with the setter function
// Return both state and ref, along with the setter function.
return [state, ref, setSyncedState] as const;
}

Expand Down

0 comments on commit 4c7ac30

Please sign in to comment.