react-use/src/useUpsert.ts
xobotyi 1840b577e2 feat(useList): reimplemented useList hook;
feat(useList): new action upsert;
feat(useList): new action update;
feat(useList): new action updateFirst;
feat(useList): new action insertAt;
feat(useList): action remove renamed to removeAt (ref remained);
feat(useUpsert): useUpsert hook deprecated cause of duplicate functionality and bad naming;
2019-11-06 14:49:33 +03:00

27 lines
671 B
TypeScript

import useList, { ListActions } from './useList';
import { InitialHookState } from './util/resolveHookState';
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: InitialHookState<T[]> = []
): [T[], UpsertListActions<T>] {
const [list, listActions] = useList(initialList);
return [
list,
{
...listActions,
upsert: (newItem: T) => {
listActions.upsert(predicate, newItem);
},
} as UpsertListActions<T>,
];
}