From 4a3cfcb7870556836cdbb83e4b8b82f5e2508d5b Mon Sep 17 00:00:00 2001 From: Konstantin Kharlamov Date: Wed, 11 Dec 2024 23:15:33 +0300 Subject: [PATCH] Document what `useRef` may be used for and how Fixes: https://github.com/purescript-react/purescript-react-basic-hooks/issues/77 --- src/React/Basic/Hooks.purs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/React/Basic/Hooks.purs b/src/React/Basic/Hooks.purs index 64537d5..86dc92b 100644 --- a/src/React/Basic/Hooks.purs +++ b/src/React/Basic/Hooks.purs @@ -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 @@ -575,4 +597,4 @@ foreign import useSyncExternalStore3_ :: forall a. EffectFn3 (EffectFn1 (Effect Unit) (Effect Unit)) -- subscribe (Effect a) -- getSnapshot (Effect a) -- getServerSnapshot - a \ No newline at end of file + a pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy