Skip to content

Commit 4a3cfcb

Browse files
committed
Document what useRef may be used for and how
Fixes: purescript-react#77
1 parent be0133c commit 4a3cfcb

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/React/Basic/Hooks.purs

+23-1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,28 @@ useReducer initialState (Reducer reducer) =
331331

332332
foreign import data UseReducer :: Type -> Type -> Type -> Type
333333

334+
-- | Typically used to create a reference to a DOM element. Usage example:
335+
-- |
336+
-- | ```purs
337+
-- | type NodeRef = Ref (Nullable Node)
338+
-- |
339+
-- | newNodeRef :: React.Hook (React.UseRef (Nullable Node)) NodeRef
340+
-- | newNodeRef = React.useRef null
341+
-- |
342+
-- | myComponent :: Component {}
343+
-- | myComponent = component "MyComponent" \_ -> React.do
344+
-- | dialogRef :: NodeRef <- newNodeRef
345+
-- | let myOnClose = resetDialog dialogRef
346+
-- | pure $
347+
-- | R.dialog
348+
-- | { ref: dialogRef
349+
-- | , children: [ R.form { ref: dialogRef } ]
350+
-- | , onClose: myOnClose
351+
-- | }
352+
-- | ```
353+
-- |
354+
-- | Here, the reference is being connected with the `dialog` via `ref` field, and at
355+
-- | the same time it's passed to `resetDialog` event handler.
334356
useRef :: forall a. a -> Hook (UseRef a) (Ref a)
335357
useRef initialValue =
336358
unsafeHook do
@@ -575,4 +597,4 @@ foreign import useSyncExternalStore3_ :: forall a. EffectFn3
575597
(EffectFn1 (Effect Unit) (Effect Unit)) -- subscribe
576598
(Effect a) -- getSnapshot
577599
(Effect a) -- getServerSnapshot
578-
a
600+
a

0 commit comments

Comments
 (0)