From 1e80aec3f32da1eb739405985211355529816bca Mon Sep 17 00:00:00 2001 From: Eric P Date: Mon, 25 May 2020 12:32:58 -0700 Subject: [PATCH] Stick with reducer Using reducer instead of state hook because of possible bug with state hooks (https://github.com/briskml/brisk-reconciler/issues/74). --- src/UI_Components/Input.re | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/UI_Components/Input.re b/src/UI_Components/Input.re index ef411d754..fe38616fa 100644 --- a/src/UI_Components/Input.re +++ b/src/UI_Components/Input.re @@ -52,6 +52,14 @@ type state = { cursorPosition: int, }; +type action = + | TextInput(string, int); + +let reducer = (action, state) => + switch (action) { + | TextInput(value, cursorPosition) => {value, cursorPosition} + }; + let getStringParts = (index, str) => { switch (index) { | 0 => ("", str) @@ -196,11 +204,18 @@ let%component make = ~isPassword=false, (), ) => { - let%hook (state, setState) = - Hooks.state({ - value: Option.value(value, ~default=""), - cursorPosition: Option.value(cursorPosition, ~default=0), - }); + // TODO: This ought to be a state hook not reducer but + // a bug with stale hook setters might happen. + // + // Refactor when https://github.com/briskml/brisk-reconciler/issues/74 has been fixed + let%hook (state, dispatch) = + Hooks.reducer( + ~initialState={ + value: Option.value(value, ~default=""), + cursorPosition: Option.value(cursorPosition, ~default=0), + }, + reducer, + ); let%hook textRef = Hooks.ref(None); let%hook scrollOffset = Hooks.ref(0); @@ -280,7 +295,7 @@ let%component make = // Refactor when https://github.com/briskml/brisk-reconciler/issues/54 has been fixed let update = (value, cursorPosition) => { onChange(value, cursorPosition); - setState(_ => {value, cursorPosition}); + dispatch(TextInput(value, cursorPosition)); }; let handleTextInput = (event: NodeEvents.textInputEventParams) => {