MCPcopy Create free account
hub / github.com/awkweb/react-supabase / useInsert

Function useInsert

src/hooks/data/use-insert.ts:31–65  ·  view source on GitHub ↗
(
    table: string,
    config: UseInsertConfig = { options: {} },
)

Source from the content-addressed store, hash-verified

29}
30
31export function useInsert<Data = any>(
32 table: string,
33 config: UseInsertConfig = { options: {} },
34): UseInsertResponse<Data> {
35 const client = useClient()
36 const isMounted = useRef(false)
37 const [state, setState] = useState<UseInsertState>(initialState)
38
39 /* eslint-disable react-hooks/exhaustive-deps */
40 const execute = useCallback(
41 async (
42 values: Partial<Data> | Partial<Data>[],
43 options?: UseInsertOptions,
44 ) => {
45 setState({ ...initialState, fetching: true })
46 const { count, data, error } = await client
47 .from<Data>(table)
48 .insert(values, options ?? config.options)
49 const res = { count, data, error }
50 if (isMounted.current) setState({ ...res, fetching: false })
51 return res
52 },
53 [client],
54 )
55 /* eslint-enable react-hooks/exhaustive-deps */
56
57 useEffect(() => {
58 isMounted.current = true
59 return () => {
60 isMounted.current = false
61 }
62 }, [])
63
64 return [state, execute]
65}

Callers 1

Calls 1

useClientFunction · 0.90

Tested by

no test coverage detected