mirror of
https://github.com/rowtype-yoga/ry-blocks.git
synced 2024-09-11 21:38:06 +03:00
Add useStateEq' hook
This commit is contained in:
parent
4a00ac00fe
commit
6e5c06746e
29
src/Yoga/Block/Hook/UseStateEq.purs
Normal file
29
src/Yoga/Block/Hook/UseStateEq.purs
Normal file
@ -0,0 +1,29 @@
|
||||
module Yoga.Block.Hook.UseStateEq where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Data.Newtype (class Newtype)
|
||||
import Data.Tuple.Nested (type (/\), (/\))
|
||||
import Effect (Effect)
|
||||
import React.Basic.Hooks (UseState, Hook, coerceHook)
|
||||
import React.Basic.Hooks as React
|
||||
|
||||
newtype UseStateEq a hooks = UseStateEq (UseState a hooks)
|
||||
|
||||
derive instance Newtype (UseStateEq a hooks) _
|
||||
|
||||
-- [TODO] I'm too stupid to write this
|
||||
-- useStateEq :: forall a. Eq a => a -> Hook (UseStateEq a) (a /\ ((a -> a) -> Effect Unit))
|
||||
-- useStateEq initialValue = coerceHook React.do
|
||||
-- value /\ update <- React.useState initialValue
|
||||
-- let
|
||||
-- updateEq modify = do
|
||||
-- modified <- modify value
|
||||
-- when (modified /= value) do update (const modified)
|
||||
-- pure (value /\ updateEq)
|
||||
|
||||
useStateEq' :: forall a. Eq a => a -> Hook (UseStateEq a) (a /\ (a -> Effect Unit))
|
||||
useStateEq' initialValue = coerceHook React.do
|
||||
value /\ setValue <- React.useState' initialValue
|
||||
let updateEq newValue = do when (newValue /= value) do setValue newValue
|
||||
pure (value /\ updateEq)
|
Loading…
Reference in New Issue
Block a user