@codonsplice/react 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +26 -0
- package/package.json +9 -0
package/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @codonsplice/react — a useSpliceQL() hook over @codonsplice/wasm.
|
|
2
|
+
import { useState, useCallback } from 'react'
|
|
3
|
+
import { execute as csExecute } from '../index.js'
|
|
4
|
+
|
|
5
|
+
export function useSpliceQL() {
|
|
6
|
+
const [result, setResult] = useState(null)
|
|
7
|
+
const [error, setError] = useState(null)
|
|
8
|
+
const [loading, setLoading] = useState(false)
|
|
9
|
+
|
|
10
|
+
const execute = useCallback(async ({ query, files, vars }) => {
|
|
11
|
+
setLoading(true)
|
|
12
|
+
setError(null)
|
|
13
|
+
try {
|
|
14
|
+
const r = await csExecute({ query, files, vars })
|
|
15
|
+
setResult(r)
|
|
16
|
+
return r
|
|
17
|
+
} catch (e) {
|
|
18
|
+
setError(e)
|
|
19
|
+
throw e
|
|
20
|
+
} finally {
|
|
21
|
+
setLoading(false)
|
|
22
|
+
}
|
|
23
|
+
}, [])
|
|
24
|
+
|
|
25
|
+
return { execute, result, error, loading }
|
|
26
|
+
}
|