-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adaptive.Hooks.fs
32 lines (26 loc) · 994 Bytes
/
Adaptive.Hooks.fs
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
module Adaptive.Hooks
open Fable.React
open FSharp.Data.Adaptive
[<RequireQualifiedAccess>]
module Hook =
/// Use adaptive value (re-render component when value changes)
/// returns: state: 'T
let useAVal(v: aval<'T>): 'T =
let stateHook = Hooks.useStateLazy (fun () -> AVal.force v)
Hooks.useEffectDisposable(
fun () -> v.AddCallback(
fun _ -> stateHook.update(AVal.force v)
)
)
stateHook.current
/// Use changeable adaptive value (re-render component when value changes)
/// returns: state: 'T * setState: 'T -> unit
let useCVal(v: cval<'T>): 'T * ('T -> unit) =
let stateHook = Hooks.useStateLazy (fun () -> AVal.force v)
Hooks.useEffectDisposable(
fun () -> v.AddCallback(
fun _ -> stateHook.update(AVal.force v)
)
)
let setCState newVal = transact (fun () -> v.Value <- newVal)
stateHook.current, setCState