Skip to content

Commit c8ddc89

Browse files
author
Madeline Trotter
committed
Add Render aliases (Hook, Pure) to clean up types
1 parent e14e753 commit c8ddc89

File tree

3 files changed

+25
-38
lines changed

3 files changed

+25
-38
lines changed

examples/controlled-input/src/ControlledInput.purs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Data.Maybe (Maybe(..), fromMaybe, maybe)
77
import React.Basic.DOM as R
88
import React.Basic.DOM.Events (preventDefault, stopPropagation, targetValue, timeStamp)
99
import React.Basic.Events (EventHandler, handler, merge)
10-
import React.Basic.Hooks (CreateComponent, Render, UseState, component, fragment, useState, (/\))
10+
import React.Basic.Hooks (CreateComponent, UseState, Hook, component, fragment, useState, (/\))
1111
import React.Basic.Hooks as React
1212

1313
mkControlledInput :: CreateComponent {}
@@ -29,11 +29,9 @@ mkControlledInput = do
2929
]
3030

3131
useInput
32-
:: forall hooks
33-
. String
34-
-> Render
35-
hooks
36-
(UseState { value :: String, lastChanged :: Maybe Number } hooks)
32+
:: String
33+
-> Hook
34+
(UseState { value :: String, lastChanged :: Maybe Number })
3735
{ onChange :: EventHandler
3836
, value :: String
3937
, lastChanged :: Maybe Number

examples/refs/src/Refs.purs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Data.Maybe (Maybe(..))
77
import Data.Nullable (Nullable, null)
88
import Math (pow, sqrt)
99
import React.Basic.DOM as R
10-
import React.Basic.Hooks (CreateComponent, Ref, Render, UseEffect, UseRef, UseState, Tuple, component, element, fragment, readRefMaybe, useEffect, useRef, useState, (/\))
10+
import React.Basic.Hooks (CreateComponent, Ref, Tuple, UseEffect, UseRef, UseState, Hook, component, element, fragment, readRefMaybe, useEffect, useRef, useState, (/\))
1111
import React.Basic.Hooks as React
1212
import Unsafe.Coerce (unsafeCoerce)
1313
import Web.DOM (Node)
@@ -46,12 +46,7 @@ mkRefs = do
4646

4747
type UseNodeDistance hooks = UseEffect (UseState Int (UseRef (Nullable Node) hooks))
4848

49-
useNodeDistanceFromMouse
50-
:: forall hooks
51-
. Render
52-
hooks
53-
(UseNodeDistance hooks)
54-
(Tuple Int (Ref (Nullable Node)))
49+
useNodeDistanceFromMouse :: Hook UseNodeDistance (Tuple Int (Ref (Nullable Node)))
5550
useNodeDistanceFromMouse = React.do
5651
elementRef <- useRef null
5752
mouseDistance /\ setMouseDistance <- useState 0

src/React/Basic/Hooks.purs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ module React.Basic.Hooks
4343
, toKey
4444
, unsafeToKey
4545
, Render
46+
, Pure
47+
, Hook
4648
, bind
4749
, discard
4850
, pure
@@ -96,38 +98,36 @@ memo = flip Prelude.bind (runEffectFn1 memo_)
9698
foreign import data UseState :: Type -> Type -> Type
9799

98100
useState
99-
:: forall hooks state
101+
:: forall state
100102
. state
101-
-> Render hooks (UseState state hooks) (Tuple state ((state -> state) -> Effect Unit))
103+
-> Hook (UseState state) (Tuple state ((state -> state) -> Effect Unit))
102104
useState initialState = Render do
103105
runEffectFn2 useState_ (mkFn2 Tuple) initialState
104106

105107
foreign import data UseEffect :: Type -> Type
106108

107109
useEffect
108-
:: forall hooks
109-
. Array Key
110+
:: Array Key
110111
-> Effect (Effect Unit)
111-
-> Render hooks (UseEffect hooks) Unit
112+
-> Hook UseEffect Unit
112113
useEffect keys effect = Render (runEffectFn2 useEffect_ effect keys)
113114

114115
foreign import data UseLayoutEffect :: Type -> Type
115116

116117
useLayoutEffect
117-
:: forall hooks
118-
. Array Key
118+
:: Array Key
119119
-> Effect (Effect Unit)
120-
-> Render hooks (UseLayoutEffect hooks) Unit
120+
-> Hook UseLayoutEffect Unit
121121
useLayoutEffect keys effect = Render (runEffectFn2 useLayoutEffect_ effect keys)
122122

123123
foreign import data UseReducer :: Type -> Type -> Type -> Type
124124

125125
useReducer
126-
:: forall hooks state action
126+
:: forall state action
127127
. ToKey state
128128
=> state
129129
-> (state -> action -> state)
130-
-> Render hooks (UseReducer state action hooks) (Tuple state (action -> Effect Unit))
130+
-> Hook (UseReducer state action) (Tuple state (action -> Effect Unit))
131131
useReducer initialState reducer = Render do
132132
runEffectFn3 useReducer_
133133
(mkFn2 Tuple)
@@ -138,10 +138,7 @@ foreign import data UseRef :: Type -> Type -> Type
138138

139139
foreign import data Ref :: Type -> Type
140140

141-
useRef
142-
:: forall hooks a
143-
. a
144-
-> Render hooks (UseRef a hooks) (Ref a)
141+
useRef :: forall a . a -> Hook (UseRef a) (Ref a)
145142
useRef initialValue = Render do
146143
runEffectFn1 useRef_ initialValue
147144

@@ -154,20 +151,17 @@ readRefMaybe a = map toMaybe (readRef a)
154151
writeRef :: forall a. Ref a -> a -> Effect Unit
155152
writeRef = runEffectFn2 writeRef_
156153

157-
renderRef :: forall hooks a. Ref a -> Render hooks hooks a
154+
renderRef :: forall a. Ref a -> Pure a
158155
renderRef ref = Render (readRef ref)
159156

160-
renderRefMaybe :: forall hooks a. Ref (Nullable a) -> Render hooks hooks (Maybe a)
157+
renderRefMaybe :: forall a. Ref (Nullable a) -> Pure (Maybe a)
161158
renderRefMaybe a = Render (readRefMaybe a)
162159

163160
foreign import data UseContext :: Type -> Type -> Type
164161

165162
foreign import data Context :: Type -> Type
166163

167-
useContext
168-
:: forall hooks a
169-
. Context a
170-
-> Render hooks (UseContext a hooks) (Maybe a)
164+
useContext :: forall a . Context a -> Hook (UseContext a) (Maybe a)
171165
useContext context = Render (map toMaybe (runEffectFn1 useContext_ context))
172166

173167
createContext :: forall a. a -> Effect (Context a)
@@ -178,11 +172,7 @@ contextProvider context a child = element (contextProvider_ context) { value: a,
178172

179173
foreign import data UseMemo :: Type -> Type -> Type
180174

181-
useMemo
182-
:: forall hooks a
183-
. Array Key
184-
-> (Unit -> a)
185-
-> Render hooks (UseMemo a hooks) a
175+
useMemo :: forall a . Array Key -> (Unit -> a) -> Hook (UseMemo a) a
186176
useMemo keys factory = Render (runEffectFn2 useMemo_ factory keys)
187177

188178
-- | Keys represent values React uses to check for changes.
@@ -230,6 +220,10 @@ instance trMaybe :: ToKey (Maybe a) where
230220
-- | effects.
231221
newtype Render x y a = Render (Effect a)
232222

223+
type Pure a = forall hooks. Render hooks hooks a
224+
225+
type Hook (newHook :: Type -> Type) a = forall hooks. Render hooks (newHook hooks) a
226+
233227
instance ixFunctorRender :: IxFunctor Render where
234228
imap f (Render a) = Render (map f a)
235229

0 commit comments

Comments
 (0)