mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2025-12-24 20:26:24 +00:00
Confine message selector cache to component
This commit is contained in:
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]
|
||||
)
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user