-
Notifications
You must be signed in to change notification settings - Fork 21
/
react.fs
80 lines (62 loc) · 3.44 KB
/
react.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
namespace Elmish.React
[<AutoOpen>]
module Helpers =
open Fable.React.Props
open Fable.Core.JsInterop
/// `Ref` callback that sets the value of an input textbox after DOM element is created.
/// Can be used instead of `DefaultValue` and `Value` props to override input box value.
let inline valueOrDefault value =
Ref <| (fun e -> if e |> isNull |> not && !!e?value <> !!value then e?value <- !!value)
[<RequireQualifiedAccess>]
module Program =
module Internal =
open Fable.React
open Browser
open Elmish
let withReactBatchedUsing lazyView2With placeholderId (program:Program<_,_,_,_>) =
let mutable lastRequest = None
let setState model dispatch =
match lastRequest with
| Some r -> window.cancelAnimationFrame r
| _ -> ()
lastRequest <- Some (window.requestAnimationFrame (fun _ ->
ReactDom.render(
lazyView2With (fun x y -> obj.ReferenceEquals(x,y)) (Program.view program) model dispatch,
document.getElementById placeholderId
)))
program
|> Program.withSetState setState
let withReactSynchronousUsing lazyView2With placeholderId (program:Elmish.Program<_,_,_,_>) =
let setState model dispatch =
ReactDom.render(
lazyView2With (fun x y -> obj.ReferenceEquals(x,y)) (Program.view program) model dispatch,
document.getElementById placeholderId
)
program
|> Program.withSetState setState
let withReactHydrateUsing lazyView2With placeholderId (program:Elmish.Program<_,_,_,_>) =
let setState model dispatch =
ReactDom.hydrate(
lazyView2With (fun x y -> obj.ReferenceEquals(x,y)) (Program.view program) model dispatch,
document.getElementById placeholderId
)
program
|> Program.withSetState setState
/// Renders React root component inside html element identified by placeholderId.
/// Uses `requestAnimationFrame` to batch updates to prevent drops in frame rate.
/// NOTE: This may have unexpected effects in React controlled inputs, see https://github.com/elmish/react/issues/12
let withReactBatched placeholderId (program:Elmish.Program<_,_,_,_>) =
Internal.withReactBatchedUsing lazyView2With placeholderId program
/// Renders React root component inside html element identified by placeholderId.
/// New renders are triggered immediately after an update.
let withReactSynchronous placeholderId (program:Elmish.Program<_,_,_,_>) =
Internal.withReactSynchronousUsing lazyView2With placeholderId program
[<System.Obsolete("Use withReactBatched")>]
let withReact placeholderId (program:Elmish.Program<_,_,_,_>) =
Internal.withReactBatchedUsing lazyView2With placeholderId program
[<System.Obsolete("Use withReactSynchronous")>]
let withReactUnoptimized placeholderId (program:Elmish.Program<_,_,_,_>) =
Internal.withReactSynchronousUsing lazyView2With placeholderId program
/// Renders React root component inside html element identified by placeholderId using `React.hydrate`.
let withReactHydrate placeholderId (program:Elmish.Program<_,_,_,_>) =
Internal.withReactHydrateUsing lazyView2With placeholderId program