Skip to content

Commit

Permalink
fix: Reduce size of useControllerState().
Browse files Browse the repository at this point in the history
  • Loading branch information
jwalton committed Aug 10, 2021
1 parent 6016c69 commit 12a4297
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ export function useControllerState<S, R = Immutable<S>>(
}

// Update resultRef and force a re-render if the store changes.
useEffect(() => {
const unsubscribe = store.subscribe((state: Immutable<S>) => {
const newResult = latestSelector.current(state);
if (!latestIsEqual.current(resultRef.current, newResult)) {
resultRef.current = newResult;
setResult(newResult);
}
});

return () => unsubscribe();
}, [latestIsEqual, latestSelector, store]);
useEffect(
() =>
store.subscribe((state: Immutable<S>) => {
const newResult = latestSelector.current(state);
if (!latestIsEqual.current(resultRef.current, newResult)) {
resultRef.current = newResult;
setResult(newResult);
}
}),
[latestIsEqual, latestSelector, store]
);

return resultRef.current;
}
Expand Down

0 comments on commit 12a4297

Please sign in to comment.