-
Notifications
You must be signed in to change notification settings - Fork 21
/
react-native.fs
55 lines (44 loc) · 1.79 KB
/
react-native.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
namespace Elmish.ReactNative
open Fable.React
open Fable.Core
open Elmish
module Components =
type AppState = {
render : unit -> ReactElement
setState : AppState -> unit
}
let mutable appState = None
type App(props) as this =
inherit Component<obj,AppState>(props)
do
match appState with
| Some state ->
appState <- Some { state with AppState.setState = this.setInitState }
this.setInitState state
| _ -> failwith "was Elmish.ReactNative.Program.withReactNative called?"
override this.componentDidMount() =
appState <- Some { appState.Value with setState = fun s -> this.setState(fun _ _ -> s) }
override this.componentWillUnmount() =
appState <- Some { appState.Value with setState = ignore; render = this.state.render }
override this.render () =
this.state.render()
[<Import("AppRegistry","react-native")>]
type AppRegistry =
static member registerComponent(appKey:string, getComponentFunc:unit->ReactElementType<_>) : unit =
failwith "JS only"
[<RequireQualifiedAccess>]
module Program =
open Elmish.React
open Components
/// Setup rendering of root ReactNative component
let withReactNative appKey (program:Program<_,_,_,_>) =
AppRegistry.registerComponent(appKey, fun () -> unbox JsInterop.jsConstructor<App>)
let setState m d =
match appState with
| Some state ->
state.setState { state with render = fun () -> (Program.view program) m d }
| _ ->
appState <- Some { render = fun () -> (Program.view program) m d
setState = ignore }
program
|> Program.withSetState setState