Confine message selector cache to component
This commit is contained in:
parent
7f0ed2599d
commit
ef13eb06fc
11 changed files with 395 additions and 311 deletions
23
ts/hooks/useProxySelector.ts
Normal file
23
ts/hooks/useProxySelector.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
// Copyright 2023 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { useCallback, useMemo } from 'react';
|
||||
import { useSelector } from 'react-redux';
|
||||
import { memoize } from '@indutny/sneequals';
|
||||
|
||||
import type { StateType } from '../state/reducer';
|
||||
|
||||
export function useProxySelector<Params extends Array<unknown>, Result>(
|
||||
selector: (state: StateType, ...params: Params) => Result,
|
||||
...params: Params
|
||||
): Result {
|
||||
const memoized = useMemo(() => memoize(selector), [selector]);
|
||||
|
||||
return useSelector(
|
||||
useCallback(
|
||||
(state: StateType) => memoized(state, ...params),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[memoized, ...params]
|
||||
)
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue