Module

Halogen.Hooks.Extra.Hooks.UseStateFn

Idea and implementation by Thomas Honeyman. This was copied over from the Halogen Hooks repository's Examples folder.

#useStateFn

useStateFn :: forall m a b. (StateId a -> b) -> a -> Hook m (UseStateFn a) (Tuple a b)

useStateFn allows you to choose a MonadState function to pair with Hooks.useState so you don't have to keep re-typing these functions in your code if you only need to use one of them per piece of state.

The available functions to choose from are:

  • Hooks.modify_
  • Hooks.modify
  • Hooks.put
  • Hooks.get

For example, rather than writing:

count /\ countIdx <- Hooks.useState 42
-- ...
Hooks.modify_ countIdx (add 1)

You can write:

count /\ modifyCount <- useStateFn Hooks.modify_ 42
-- ...
modifyCount (add 1)

See these helper functions for another layer of convenience:

  • useModifyState_
  • useModifyState
  • usePutState

#UseStateFn

#useModifyState_

useModifyState_ :: forall m a. a -> Hook m (UseStateFn a) (Tuple a ((a -> a) -> HookM m Unit))

Just like useState, but provides a convenience function for updating state, rather than a state index to pass to Hooks.modify_.

Example:

count /\ modifyCount <- useModifyState_ 42
-- ...
modifyCount (add 1)

Instead of:

count /\ countIdx <- Hooks.useState 42
-- ...
Hooks.modify_ countIdx (add 1)

Shorthand for:

useStateFn Hooks.modify_

#useModifyState

useModifyState :: forall m a. a -> Hook m (UseStateFn a) (Tuple a ((a -> a) -> HookM m a))

Just like useState, but provides a convenience function for updating state, rather than a state index to pass to Hooks.modify.

Example:

count /\ modifyCount <- useModifyState 42
-- ...
newCount <- modifyCount (add 1)

Instead of:

count /\ countIdx <- Hooks.useState 42
-- ...
newCount <- Hooks.modify countIdx (add 1)

Shorthand for:

useStateFn Hooks.modify

#usePutState

usePutState :: forall m a. a -> Hook m (UseStateFn a) (Tuple a (a -> HookM m Unit))

Just like useState, but provides a convenience function for setting state, rather than a state index to pass to Hooks.put.

Example:

count /\ putCount <- usePutState 42
-- ...
putCount 0

Instead of:

count /\ countIdx <- Hooks.useState 42
-- ...
Hooks.put countIdx 0

Shorthand for:

useStateFn Hooks.put

Modules