mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
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;
27 lines
671 B
TypeScript
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>,
|
|
];
|
|
}
|