# FRP-ish / Reactive programming inspired by React hooks

**URL:** <https://discourse.haskell.org/t/frp-ish-reactive-programming-inspired-by-react-hooks/1759>\
**Category:** Show and Tell\
**Created:** [January 8, 2021, 4:39pm UTC](https://discourse.haskell.org/t/frp-ish-reactive-programming-inspired-by-react-hooks/1759 "2021-01-08T16:39:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![benweitzman](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/benweitzman/32/1052_2.png) [@benweitzman](https://discourse.haskell.org/u/benweitzman)\
**Post date:** [January 8, 2021, 4:39pm UTC](https://discourse.haskell.org/t/frp-ish-reactive-programming-inspired-by-react-hooks/1759/1 "2021-01-08T16:39:37Z")

</div>

I’m excited to announce a pre-release of a new library called `hooks`:

> **[GitHub - benweitzman/hooks: FRP-ish / Reactive programming inspired by React...](https://github.com/benweitzman/hooks)**
>
> FRP-ish / Reactive programming inspired by React hooks - GitHub - benweitzman/hooks: FRP-ish / Reactive programming inspired by React hooks

The goal of this library is to make it easy to write programs that produces streams of values, with do notation being used to combine the latest values from multiple streams. This library is basically a port of React Hooks to Haskell, so hopefully the model should be familiar to those who’ve worked with React Hooks.

Really excited to be working with the `QualifiedDo` extension in this library! A quick sample of how the library works:

```haskell
{-# LANGUAGE QualifiedDo #-}

-- increment a counter steadily every interval (microseconds)
useTick :: Int -> Hooks _ Int
useTick interval = Hook.do
  (tick, setTick) <- Use (State 0)

  Use $ Effect interval $ do
    thread <- async $ forever $ do
      threadDelay interval
      setTick $ Modify (+1)
    return $ cancel thread

  Hook.return tick 

-- create a variable number of counters incrementing at different speeds 
testProg :: Hooks _ [Int]
testProg = Hook.do
  (num, updateNum) <- Use $ State (0 :: Int)

  Use $ Effect () $ do -- read a number from the command line
    thread <- async . forever $ do
      x <- getLine
      updateNum . Set $ read x
    return $ cancel thread

  Use $ Map () [1..num] $ \i -> Hook.do
    (randomOffset, updateRandomOffset) <- Use $ State 0
    Use $ Effect () $ do
      randomValue <- randomRIO (-1000000, 1000000)
      updateRandomOffset $ Set randomValue
      return $ return ()
    useTick (1000000 + randomOffset)

```

Any feedback is much appreciated 😃

---

<div class="post-metadata">

**Author:** ![jamesbrock](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.haskell.org/jamesbrock/32/1404_2.png) [@jamesbrock](https://discourse.haskell.org/u/jamesbrock)\
**Post date:** [June 6, 2021, 10:02am UTC](https://discourse.haskell.org/t/frp-ish-reactive-programming-inspired-by-react-hooks/1759/2 "2021-06-06T10:02:10Z")

</div>

You’ve probably seen [https://github.com/spicydonuts/purescript-react-basic-hooks](https://github.com/spicydonuts/purescript-react-basic-hooks) right? If not you’d be interested, it’s a similar approach, but it uses an indexed monad for the `Hook` type.
