react-basic-hooks adds React hook support to react-basic!
Note: This API relies on recent React versions (>= 16.8.0). For more info on hooks, see React's documentation.
I recommend using PureScript's "qualified do" syntax while using this library (it's used in the examples, the React.do bits).
It became available in the 0.12.2 compiler release.
This library provides the React.Basic.Hooks module, which can completely replace the React.Basic module.
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.
If we prefer this API over the existing react-basic API, we may eventually replace React.Basic with this implementation.
mkCounter :: Effect (ReactComponent {})
mkCounter = do
component "Counter" \props -> React.do
counter /\ setCounter <- useState 0
pure
$ R.button
{ onClick: handler_ do
setCounter (_ + 1)
, children:
[ R.text $ "Increment: " <> show counter ]
}More examples:
- Counter with an effect
- Reducer/action-style
- Controlled inputs
- Components: Parent and Child
- Refs to DOM nodes (and extracting hook logic from a component for reuse)
- A Todo App (components, inputs, state)
- Context (creating and consuming React context)
- Aff (rendering async data, using error boundaries)
- Suspense (experimental, React Suspense demo -- similar to the Aff example, but the loading state is managed by the parent instead of the detail rendering component)