Skip to content

Commit

Permalink
Small tweak to useState/setValue usage in useReactiveVar.
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn committed May 5, 2021
1 parent b7512ea commit dfd159e
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/react/hooks/useReactiveVar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ export function useReactiveVar<T>(rv: ReactiveVar<T>): T {

// We don't actually care what useState thinks the value of the variable
// is, so we take only the update function from the returned array.
const [, setValue] = useState(value);
const setValue = useState(value)[1];

// We subscribe to variable updates on initial mount and when the value has
// changed. This avoids a subtle bug in React.StrictMode where multiple listeners
// are added, leading to inconsistent updates.
// changed. This avoids a subtle bug in React.StrictMode where multiple
// listeners are added, leading to inconsistent updates.
useEffect(() => {
const probablySameValue = rv();
if (value !== probablySameValue) {
Expand Down

0 comments on commit dfd159e

Please sign in to comment.