You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+145-2Lines changed: 145 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,21 @@
2
2
3
3
`react-basic-hooks` is a React hook API for [react-basic](https://github.com/lumihq/purescript-react-basic).
4
4
5
-
_Note:_ This API relies on React `>=16.8.0`. For more info on hooks, see [React's documentation](https://reactjs.org/docs/hooks-intro.html).
5
+
_Note:_ This library supports React `>=16.8.0` with full React 19 support. For more info on hooks, see [React's documentation](https://react.dev/reference/react).
6
6
7
-
I recommend using PureScript's "qualified do" syntax while using this library (it's used in the examples, the `React.do` bits).
7
+
I recommend using PureScript's "qualified do" syntax whilst using this library (it's used in the examples, the `React.do` bits).
8
8
It became available in the `0.12.2` compiler release.
9
9
10
10
This library provides the `React.Basic.Hooks` module, which can completely replace the `React.Basic` module.
11
11
It borrows a few types from the current `React.Basic` module like `ReactComponent` and `JSX` to make it easy to use both versions in the same project.
12
12
If we prefer this API over the existing react-basic API, we may eventually replace `React.Basic` with this implementation.
Optimistically update the UI whilst waiting for an async action to complete. The optimistic state automatically reverts to the actual state when the action finishes.
pure $ R.div_ (map renderMessage optimisticMessages)
58
+
```
59
+
60
+
### useActionState
61
+
62
+
Manage form actions with built-in pending state. The action function receives the previous state and form data, making it ideal for form submissions. Uses Effect for synchronous operations.
63
+
64
+
```purs
65
+
mkForm :: Component Unit
66
+
mkForm = do
67
+
component "Form" \_ -> React.do
68
+
state /\ (formAction /\ isPending) <- useActionState initialState updateFn
69
+
where
70
+
updateFn prevState formData = do
71
+
-- Process form submission (Effect version)
72
+
result <- submitToServer formData
73
+
pure (Result.fromEither result)
74
+
75
+
pure $ R.button
76
+
{ disabled: isPending
77
+
, onClick: handler_ (formAction myFormData)
78
+
, children: [ R.text if isPending then "Submitting..." else "Submit" ]
79
+
}
80
+
```
81
+
82
+
For progressive enhancement (form works without JavaScript), use `useActionStateWithPermalink`:
83
+
84
+
```purs
85
+
state /\ (formAction /\ isPending) <- useActionStateWithPermalink initialState updateFn "/api/submit"
86
+
87
+
pure $ R.form
88
+
{ action: formAction -- Falls back to /api/submit without JS
89
+
, children: [ ... ]
90
+
}
91
+
```
92
+
93
+
### useAffActionState
94
+
95
+
Aff version of `useActionState` for async operations. Available in `React.Basic.Hooks.Aff`. Uses Aff for natural async handling.
96
+
97
+
```purs
98
+
import React.Basic.Hooks.Aff (useAffActionState)
99
+
100
+
mkForm :: Component Unit
101
+
mkForm = do
102
+
component "Form" \_ -> React.do
103
+
state /\ (formAction /\ isPending) <- useAffActionState initialState affFn
104
+
where
105
+
affFn prevState formData = do
106
+
-- Process form submission (Aff version - natural async!)
107
+
result <- Aff.submitToServer formData
108
+
pure (Result.fromEither result)
109
+
110
+
pure $ R.button
111
+
{ disabled: isPending
112
+
, onClick: handler_ (formAction myFormData)
113
+
, children: [ R.text if isPending then "Submitting..." else "Submit" ]
114
+
}
115
+
```
116
+
117
+
With permalink: `useAffActionStateWithPermalink initialState affFn "/api/submit"`
118
+
119
+
### useEffectEvent
120
+
121
+
Extract non-reactive logic from Effects. The returned function can access the latest props and state without causing the Effect to re-run when those values change.
0 commit comments