|
1 | 1 | # React Halo |
2 | 2 |
|
3 | | -A Halogen-inspired wrapper around React. Work in progress. |
| 3 | +Halo is a [Halogen](https://github.com/purescript-halogen/purescript-halogen)-inspired interface and runtime for React. |
| 4 | + |
| 5 | +It is implemented as a hook: `useHalo`; and simple component helpers are included: `component` and `component_`. |
| 6 | + |
| 7 | +## Using with [Spago](https://github.com/purescript/spago) |
| 8 | + |
| 9 | +Update the additions in your `packages.dhall`: |
| 10 | + |
| 11 | +```dhall |
| 12 | +let additions = |
| 13 | + { react-halo = |
| 14 | + { dependencies = [ "aff", "free", "freeap", "react-basic-hooks", "wire" ] |
| 15 | + , repo = "https://github.com/robertdp/purescript-react-halo.git" |
| 16 | + , version = "master" |
| 17 | + } |
| 18 | + , wire = |
| 19 | + { dependencies = [ "aff", "filterable", "refs", "unsafe-reference" ] |
| 20 | + , repo = "https://github.com/robertdp/purescript-wire.git" |
| 21 | + , version = "v0.4.2" |
| 22 | + } |
| 23 | + } |
| 24 | +``` |
| 25 | + |
| 26 | +Then install with Spago: |
| 27 | + |
| 28 | +`$ spago install react-halo` |
| 29 | + |
| 30 | +## What does Halo provide? |
| 31 | + |
| 32 | +Whether you are using the hook or one of the component helpers, the main feature that Halo provides is the `eval` function. It looks like: |
| 33 | + |
| 34 | +```purescript |
| 35 | +Lifecycle props action -> HaloM props state action m a |
| 36 | +``` |
| 37 | + |
| 38 | +where `Lifecycle` is: |
| 39 | + |
| 40 | +```purescript |
| 41 | +data Lifecycle props action |
| 42 | + = Initialize props -- when the component mounts |
| 43 | + | Update props props -- when the props change |
| 44 | + | Action action -- when an action is dispatched |
| 45 | + | Finalize -- when the component unmounts |
| 46 | +``` |
| 47 | + |
| 48 | +`HaloM` is also a monad transformer, and so you can lift any monad `m` logic into `HaloM`. Just be aware that in order to run the logic, Halo requires that you `hoist` (convert) your chosen monad into `Aff` before returning it. |
| 49 | + |
| 50 | +## Hoisting |
| 51 | + |
| 52 | +```purescript |
| 53 | +hoist :: forall props state action m m'. Functor m => (m ~> m') -> HaloM props state action m ~> HaloM props state action m' |
| 54 | +``` |
| 55 | + |
| 56 | +Example: |
| 57 | + |
| 58 | +```purescript |
| 59 | +invertReaderT x = ReaderT \env -> Halo.hoist (flip runReaderT env) x |
| 60 | +``` |
| 61 | + |
| 62 | +## Working with props |
| 63 | + |
| 64 | +```purescript |
| 65 | +props :: forall props action state m. HaloM props state action m props |
| 66 | +``` |
| 67 | + |
| 68 | +Example: |
| 69 | + |
| 70 | +```purescript |
| 71 | +fireOnChange value = do |
| 72 | + { onChange } <- Halo.props |
| 73 | + onChange value |
| 74 | +``` |
| 75 | + |
| 76 | +## Working with state |
| 77 | + |
| 78 | +`HaloM` doesn't have any special interface for reading and modifying state, instead providing an instance of [MonadState](https://pursuit.purescript.org/packages/purescript-transformers/docs/Control.Monad.State.Class) for flexibility. |
| 79 | + |
| 80 | +## Subscriptions |
| 81 | + |
| 82 | +`HaloM` also provides functions for subscriptions management: |
| 83 | + |
| 84 | +```purescript |
| 85 | +subscribe :: forall props state action m. Event action -> HaloM props state action m SubscriptionId |
| 86 | +
|
| 87 | +unsubscribe :: forall m action state props. SubscriptionId -> HaloM props state action m Unit |
| 88 | +``` |
| 89 | + |
| 90 | +`Event` comes from the [Wire](https://github.com/robertdp/purescript-wire) library. |
| 91 | + |
| 92 | +There is also a version for subscriptions that want to unsubscribe themselves: |
| 93 | + |
| 94 | +```purescript |
| 95 | +subscribe' :: forall m action state props. (SubscriptionId -> Event action) -> HaloM props state action m SubscriptionId |
| 96 | +``` |
| 97 | + |
| 98 | +Any subscriptions that remain when the component is unmounted are automatically unsubscribed. This prevents requiring manual clean up in the `Finalize` lifecycle event. |
| 99 | + |
| 100 | +## Parallelism |
| 101 | + |
| 102 | +And finally, `HaloM` provides functions for creating and killing forks which run in parallel (or as useful an approximation as we can get in JavaScript): |
| 103 | + |
| 104 | +```purescript |
| 105 | +fork :: forall m action state props. HaloM props state action m Unit -> HaloM props state action m ForkId |
| 106 | +
|
| 107 | +kill :: forall m action state props. ForkId -> HaloM props state action m Unit |
| 108 | +``` |
| 109 | + |
| 110 | +Similarly to subscriptions, when the component unmounts all still-running forks will be killed. |
0 commit comments