react-use/src/useUpsert.ts
Renovate Bot a27f09fd36
chore: refactoring and rearrangement.
More DRY code. Also move non-hooks to separate directories.

BREAKING CHANGE: all `create*` factories been moved to `factory` subdirectory and in case direct import should be imported like `react-use/esm/factory/createBreakpoint`
BREAKING CHANGE: `comps` directory renamed to `component`
2021-01-30 23:30:26 +03:00

27 lines
672 B
TypeScript

import useList, { ListActions } from './useList';
import { IHookStateInitAction } from './misc/hookState';
export interface UpsertListActions<T> extends Omit<ListActions<T>, 'upsert'> {
upsert: (newItem: T) => void;
}
/**
* @deprecated Use `useList` hook's upsert action instead
*/
export default function useUpsert<T>(
predicate: (a: T, b: T) => boolean,
initialList: IHookStateInitAction<T[]> = []
): [T[], UpsertListActions<T>] {
const [list, listActions] = useList(initialList);
return [
list,
{
...listActions,
upsert: (newItem: T) => {
listActions.upsert(predicate, newItem);
},
} as UpsertListActions<T>,
];
}