@@ -0,0 +1,29 @@
1+module Yoga.Block.Hook.UseStateEq where
5+import Data.Newtype (class Newtype)
6+import Data.Tuple.Nested (type (/\), (/\))
8+import React.Basic.Hooks (UseState, Hook, coerceHook)
9+import React.Basic.Hooks as React
11+newtype UseStateEq a hooks = UseStateEq (UseState a hooks)
13+derive instance Newtype (UseStateEq a hooks) _
15+-- [TODO] I'm too stupid to write this
16+-- useStateEq :: forall a. Eq a => a -> Hook (UseStateEq a) (a /\ ((a -> a) -> Effect Unit))
17+-- useStateEq initialValue = coerceHook React.do
18+-- value /\ update <- React.useState initialValue
20+-- updateEq modify = do
21+-- modified <- modify value
22+-- when (modified /= value) do update (const modified)
23+-- pure (value /\ updateEq)
25+useStateEq' :: forall a. Eq a => a -> Hook (UseStateEq a) (a /\ (a -> Effect Unit))
26+useStateEq' initialValue = coerceHook React.do
27+ value /\ setValue <- React.useState' initialValue
28+ let updateEq newValue = do when (newValue /= value) do setValue newValue
29+ pure (value /\ updateEq)