@rpcbase/client 0.120.0 → 0.122.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 +18 -4
- package/package.json +1 -1
package/helpers/useRPC.js
CHANGED
|
@@ -1,16 +1,30 @@
|
|
|
1
1
|
/* @flow */
|
|
2
|
-
import {useState, useEffect} from "react"
|
|
2
|
+
import {useCallback, useMemo, useState, useEffect, useRef} from "react"
|
|
3
|
+
import isEqual from "fast-deep-equal"
|
|
4
|
+
|
|
5
|
+
const useRPC = (fn, payload) => {
|
|
6
|
+
const previousPayloadRef = useRef(null)
|
|
3
7
|
|
|
4
|
-
const useRPC = (fn, payload = {}) => {
|
|
5
8
|
const [loading, setLoading] = useState(true)
|
|
6
9
|
const [error, setError] = useState()
|
|
7
10
|
const [data, setData] = useState()
|
|
8
11
|
|
|
12
|
+
const memoFn = useCallback(fn, [fn])
|
|
13
|
+
const memoPayload = useMemo(() => {
|
|
14
|
+
return payload || {}
|
|
15
|
+
}, [payload])
|
|
16
|
+
|
|
9
17
|
useEffect(() => {
|
|
18
|
+
if (isEqual(previousPayloadRef.current, memoPayload)) {
|
|
19
|
+
return
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
previousPayloadRef.current = memoPayload
|
|
23
|
+
|
|
10
24
|
const load = async() => {
|
|
11
25
|
setLoading(true)
|
|
12
26
|
|
|
13
|
-
const res = await
|
|
27
|
+
const res = await memoFn(memoPayload)
|
|
14
28
|
|
|
15
29
|
setLoading(false)
|
|
16
30
|
setData(res)
|
|
@@ -19,7 +33,7 @@ const useRPC = (fn, payload = {}) => {
|
|
|
19
33
|
}
|
|
20
34
|
}
|
|
21
35
|
load()
|
|
22
|
-
}, [
|
|
36
|
+
}, [memoFn, memoPayload])
|
|
23
37
|
|
|
24
38
|
return {loading, error, data}
|
|
25
39
|
}
|