Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document what useRef may be used for and how #78

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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