-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathuseUpsert.ts
More file actions
33 lines (30 loc) · 960 Bytes
/
useUpsert.ts
File metadata and controls
33 lines (30 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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>] {
if (process.env.NODE_ENV !== 'production') {
console.warn(
'useUpsert is deprecated and will be removed in a future version. ' +
'Use useList hook\'s upsert action instead. ' +
'See: https://github.com/streamich/react-use/blob/master/docs/useUpsert.md'
);
}
const [list, listActions] = useList(initialList);
return [
list,
{
...listActions,
upsert: (newItem: T) => {
listActions.upsert(predicate, newItem);
},
} as UpsertListActions<T>,
];
}