@rpcbase/client 0.117.0 → 0.118.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/helpers/useRPC.js +27 -0
- package/index.js +4 -0
- package/package.json +1 -1
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
import {useState, useEffect} from "react"
|
|
3
|
+
|
|
4
|
+
const useRPC = (fn, payload = {}) => {
|
|
5
|
+
const [loading, setLoading] = useState(true)
|
|
6
|
+
const [error, setError] = useState()
|
|
7
|
+
const [data, setData] = useState()
|
|
8
|
+
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
const load = async() => {
|
|
11
|
+
setLoading(true)
|
|
12
|
+
|
|
13
|
+
const res = await fn(payload)
|
|
14
|
+
|
|
15
|
+
setLoading(false)
|
|
16
|
+
setData(res)
|
|
17
|
+
if (res.status !== "ok") {
|
|
18
|
+
setError(res.message)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
load()
|
|
22
|
+
}, [fn, payload])
|
|
23
|
+
|
|
24
|
+
return {loading, error, data}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export default useRPC
|
package/index.js
ADDED