Commit c2cd0e75 authored by James Laver's avatar James Laver

Made useState return a normal curried Effect function for setting

parent 02dde946
...@@ -30,12 +30,11 @@ import Reactix.React ( Hooks, react, unsafeHooksEffect ) ...@@ -30,12 +30,11 @@ import Reactix.React ( Hooks, react, unsafeHooksEffect )
--- useState --- useState
-- | A state hook is a tuple of value and setter -- | A state hook is a tuple of value and setter
type State state = Tuple state (EffectFn1 state Unit) type State state = Tuple state (state -> Effect Unit)
-- | Given an Effect function returning an initial value, returns a State -- | Given an Effect function returning an initial value, returns a State
useState :: forall s. (Unit -> Effect s) -> Hooks (State s) useState :: forall s. (Unit -> Effect s) -> Hooks (State s)
useState s = hook $ \_ -> pure $ tuple $ react ... "useState" $ [ delay s ] useState s = hook $ \_ -> pure $ currySecond $ react ... "useState" $ [ delay s ]
-- -- useReducer -- -- useReducer
-- type Reducer state action = Tuple state (EffectFn1 action Unit) -- type Reducer state action = Tuple state (EffectFn1 action Unit)
...@@ -146,8 +145,7 @@ useLayoutEffect5 a b c d f e = _useLayoutEffect e $ args5 a b c d f ...@@ -146,8 +145,7 @@ useLayoutEffect5 a b c d f e = _useLayoutEffect e $ args5 a b c d f
type Ref state = Tuple state (state -> Effect Unit) type Ref state = Tuple state (state -> Effect Unit)
useRef :: forall r. r -> Hooks (Ref r) useRef :: forall r. r -> Hooks (Ref r)
useRef r = hook $ \_ -> pure $ friendly $ tupleCurrent $ react ... "useRef" $ [ r ] useRef r = hook $ \_ -> pure $ currySecond $ tupleCurrent $ react ... "useRef" $ [ r ]
where friendly (Tuple v s) = Tuple v (runEffectFn1 s)
-- useContext -- useContext
...@@ -185,6 +183,9 @@ tupleCurrent = runFn2 _tupleCurrent Tuple ...@@ -185,6 +183,9 @@ tupleCurrent = runFn2 _tupleCurrent Tuple
foreign import _tupleCurrent :: forall a b c. Fn2 (a -> b -> Tuple a b) c (Tuple a b) foreign import _tupleCurrent :: forall a b c. Fn2 (a -> b -> Tuple a b) c (Tuple a b)
currySecond :: forall a b c. Tuple a (EffectFn1 b c) -> Tuple a (b -> Effect c)
currySecond (Tuple a b) = Tuple a (runEffectFn1 b)
hook :: forall v. (Unit -> Effect v) -> Hooks v hook :: forall v. (Unit -> Effect v) -> Hooks v
hook = unsafeHooksEffect <<< delay hook = unsafeHooksEffect <<< delay
......
...@@ -53,7 +53,7 @@ counterCpt = R.hooksComponent "Counter" cpt ...@@ -53,7 +53,7 @@ counterCpt = R.hooksComponent "Counter" cpt
pure $ div { className: "counter" } pure $ div { className: "counter" }
[ button { type: "button", onClick: onclick setY (y + 1) } [ text "++" ] [ button { type: "button", onClick: onclick setY (y + 1) } [ text "++" ]
, div {} [ text (show y) ] ] , div {} [ text (show y) ] ]
onclick set to = mkEffectFn1 $ \e -> runEffectFn1 set to onclick set to = mkEffectFn1 $ \e -> set to
counterTest :: Spec Unit counterTest :: Spec Unit
counterTest = counterTest =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment