@@ -19,76 +19,46 @@ module Program =
1919 open Browser
2020 open Elmish
2121
22- // Use the new rendering API in React 18+
23- let useRootApi = try int ReactBindings.React.version.[ .. 1 ] >= 18 with _ -> false
24-
2522 let withReactBatchedUsing lazyView2With placeholderId ( program : Program < _ , _ , _ , _ >) =
2623 let setState =
2724 let mutable lastRequest = None
25+ let root = ReactDomClient.createRoot ( document.getElementById placeholderId)
2826
29- if useRootApi then
30- let root = ReactDomClient.createRoot ( document.getElementById placeholderId)
31-
32- fun model dispatch ->
33- match lastRequest with
34- | Some r -> window.cancelAnimationFrame r
35- | _ -> ()
36-
37- lastRequest <- Some ( window.requestAnimationFrame ( fun _ ->
38- root.render ( lazyView2With ( fun x y -> obj.ReferenceEquals( x, y)) ( Program.view program) model dispatch)))
39- else
40- fun model dispatch ->
41- match lastRequest with
42- | Some r -> window.cancelAnimationFrame r
43- | _ -> ()
27+ fun model dispatch ->
28+ match lastRequest with
29+ | Some r -> window.cancelAnimationFrame r
30+ | _ -> ()
4431
45- lastRequest <- Some ( window.requestAnimationFrame ( fun _ ->
46- ReactDom.render(
47- lazyView2With ( fun x y -> obj.ReferenceEquals( x, y)) ( Program.view program) model dispatch,
48- document.getElementById placeholderId
49- )))
32+ lastRequest <- Some ( window.requestAnimationFrame ( fun _ ->
33+ root.render ( lazyView2With ( fun x y -> obj.ReferenceEquals( x, y)) ( Program.view program) model dispatch)))
5034
5135 program
5236 |> Program.withSetState setState
5337
5438 let withReactSynchronousUsing lazyView2With placeholderId ( program : Elmish.Program < _ , _ , _ , _ >) =
5539 let setState =
56- if useRootApi then
57- let root = ReactDomClient.createRoot ( document.getElementById placeholderId)
40+ let root = ReactDomClient.createRoot ( document.getElementById placeholderId)
5841
59- fun model dispatch ->
60- root.render ( lazyView2With ( fun x y -> obj.ReferenceEquals( x, y)) ( Program.view program) model dispatch)
61- else
62- fun model dispatch ->
63- ReactDom.render(
64- lazyView2With ( fun x y -> obj.ReferenceEquals( x, y)) ( Program.view program) model dispatch,
65- document.getElementById placeholderId
66- )
42+ fun model dispatch ->
43+ root.render ( lazyView2With ( fun x y -> obj.ReferenceEquals( x, y)) ( Program.view program) model dispatch)
6744
6845 program
6946 |> Program.withSetState setState
7047
7148 let withReactHydrateUsing lazyView2With placeholderId ( program : Elmish.Program < _ , _ , _ , _ >) =
7249 let setState =
73- if useRootApi then
74- let mutable root = None
75-
76- fun model dispatch ->
77- match root with
78- | None ->
79- root <-
80- ReactDomClient.hydrateRoot (
81- document.getElementById placeholderId,
82- lazyView2With ( fun x y -> obj.ReferenceEquals( x, y)) ( Program.view program) model dispatch
83- ) |> Some
84- | Some root ->
85- root.render ( lazyView2With ( fun x y -> obj.ReferenceEquals( x, y)) ( Program.view program) model dispatch)
86- else
87- fun model dispatch ->
88- ReactDom.hydrate(
89- lazyView2With ( fun x y -> obj.ReferenceEquals( x, y)) ( Program.view program) model dispatch,
90- document.getElementById placeholderId
91- )
50+ let mutable root = None
51+
52+ fun model dispatch ->
53+ match root with
54+ | None ->
55+ root <-
56+ ReactDomClient.hydrateRoot (
57+ document.getElementById placeholderId,
58+ lazyView2With ( fun x y -> obj.ReferenceEquals( x, y)) ( Program.view program) model dispatch
59+ ) |> Some
60+ | Some root ->
61+ root.render ( lazyView2With ( fun x y -> obj.ReferenceEquals( x, y)) ( Program.view program) model dispatch)
9262
9363 program
9464 |> Program.withSetState setState
0 commit comments