|
| 1 | +/* |
| 2 | + # Example with state persistance |
| 3 | +
|
| 4 | + Require to have |
| 5 | + - @reason-react-native/storage (ReactNativeAsyncStorage) |
| 6 | + - reason-future |
| 7 | +
|
| 8 | + Require to define your own |
| 9 | +
|
| 10 | + - <RootNavigator /> |
| 11 | + - <BootSplash /> |
| 12 | +
|
| 13 | +
|
| 14 | + ```reason |
| 15 | + open Belt; |
| 16 | + open ReactNative; |
| 17 | +
|
| 18 | + // @todo |
| 19 | + // <RootNavigator /> |
| 20 | + // <Bootsplash /> |
| 21 | +
|
| 22 | + type navigationState; |
| 23 | + let navigationStateStorageKey = "react-navigation:state"; |
| 24 | +
|
| 25 | + [@react.component] |
| 26 | + let app = () => { |
| 27 | + let (initialStateContainer, setInitialState) = React.useState(() => None); |
| 28 | +
|
| 29 | + React.useEffect1( |
| 30 | + () => { |
| 31 | + if (initialStateContainer->Option.isNone) { |
| 32 | + ReactNativeAsyncStorage.getItem(navigationStateStorageKey) |
| 33 | + ->FutureJs.fromPromise(error => { |
| 34 | + // @todo ? |
| 35 | + Js.log2("Restoring Navigation State: ", error); |
| 36 | + error; |
| 37 | + }) |
| 38 | + ->Future.tap(res => { |
| 39 | + switch (res) { |
| 40 | + | Result.Ok(jsonState) => |
| 41 | + switch (jsonState->Js.Null.toOption) { |
| 42 | + | Some(jsonState) => |
| 43 | + switch (Js.Json.parseExn(jsonState)) { |
| 44 | + | state => |
| 45 | + setInitialState(_ => Some(Some(state))); |
| 46 | + | exception _ => |
| 47 | + Js.log2( |
| 48 | + "Restoring Navigation State: unable to decode valid json", |
| 49 | + jsonState, |
| 50 | + ); |
| 51 | + setInitialState(_ => Some(None)); |
| 52 | + } |
| 53 | + | None => setInitialState(_ => Some(None)) |
| 54 | + }; |
| 55 | + | Result.Error(e) => |
| 56 | + Js.log2( |
| 57 | + "Restoring Navigation State: unable to get json state", |
| 58 | + e, |
| 59 | + ); |
| 60 | + setInitialState(_ => Some(None)); |
| 61 | + }; |
| 62 | + }) |
| 63 | + ->ignore; |
| 64 | + }; |
| 65 | + None; |
| 66 | + }, |
| 67 | + [|initialStateContainer|], |
| 68 | + ); |
| 69 | +
|
| 70 | + let isReady = initialStateContainer->Option.isSome; |
| 71 | + <> |
| 72 | + {initialStateContainer |
| 73 | + ->Option.map(initialState => |
| 74 | + <Native.NavigationNativeContainer |
| 75 | + ?initialState |
| 76 | + onStateChange={state => { |
| 77 | + let maybeJsonState = Js.Json.stringifyAny(state); |
| 78 | + switch (maybeJsonState) { |
| 79 | + | Some(jsonState) => |
| 80 | + AsyncStorage.setItem(navigationStateStorageKey, jsonState) |
| 81 | + ->ignore |
| 82 | + | None => Js.log("Unable to stringify navigation state") |
| 83 | + }; |
| 84 | + }}> |
| 85 | + <RootNavigator /> |
| 86 | + </Native.NavigationNativeContainer> |
| 87 | + ) |
| 88 | + ->Option.getWithDefault(React.null)} |
| 89 | + <Bootsplash isReady /> |
| 90 | + </>; |
| 91 | + }; |
| 92 | + ``` |
| 93 | + */ |
| 94 | + |
1 | 95 | module NavigationNativeContainer = {
|
2 |
| - //TODO |
3 |
| - type initialState; |
4 |
| - type navigationState; |
| 96 | + type state = Js.Json.t; |
| 97 | + type navigationState = state => unit; |
5 | 98 |
|
6 | 99 | [@bs.module "@react-navigation/native"] [@react.component]
|
7 | 100 | external make:
|
8 | 101 | (
|
9 |
| - ~initialState: initialState=?, |
| 102 | + ~initialState: state=?, |
10 | 103 | ~onStateChange: navigationState=?,
|
11 | 104 | ~children: React.element
|
12 | 105 | ) =>
|
|
0 commit comments