Skip to content

Commit

Permalink
Document what useRef may be used for and how
Browse files Browse the repository at this point in the history
  • Loading branch information
Hi-Angel committed Dec 11, 2024
1 parent be0133c commit 4a3cfcb
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/React/Basic/Hooks.purs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,28 @@ useReducer initialState (Reducer reducer) =

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

-- | Typically used to create a reference to a DOM element. Usage example:
-- |
-- | ```purs
-- | type NodeRef = Ref (Nullable Node)
-- |
-- | newNodeRef :: React.Hook (React.UseRef (Nullable Node)) NodeRef
-- | newNodeRef = React.useRef null
-- |
-- | myComponent :: Component {}
-- | myComponent = component "MyComponent" \_ -> React.do
-- | dialogRef :: NodeRef <- newNodeRef
-- | let myOnClose = resetDialog dialogRef
-- | pure $
-- | R.dialog
-- | { ref: dialogRef
-- | , children: [ R.form { ref: dialogRef } ]
-- | , onClose: myOnClose
-- | }
-- | ```
-- |
-- | Here, the reference is being connected with the `dialog` via `ref` field, and at
-- | the same time it's passed to `resetDialog` event handler.
useRef :: forall a. a -> Hook (UseRef a) (Ref a)
useRef initialValue =
unsafeHook do
Expand Down Expand Up @@ -575,4 +597,4 @@ foreign import useSyncExternalStore3_ :: forall a. EffectFn3
(EffectFn1 (Effect Unit) (Effect Unit)) -- subscribe
(Effect a) -- getSnapshot
(Effect a) -- getServerSnapshot
a
a

0 comments on commit 4a3cfcb

Please sign in to comment.