Skip to content

Commit b7bf89f

Browse files
committed
Update TimeReactHooks
1 parent 2c32f99 commit b7bf89f

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

recipes/TimeReactHooks/spago.dhall

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
, "prelude"
1212
, "react-basic-dom"
1313
, "react-basic-hooks"
14+
, "web-dom"
1415
, "web-html"
1516
]
1617
, packages = ../../packages.dhall

recipes/TimeReactHooks/src/Main.purs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,30 @@ import Data.Newtype (class Newtype)
1111
import Effect (Effect)
1212
import Effect.Exception (throw)
1313
import Effect.Timer (clearInterval, setInterval)
14-
import React.Basic.DOM (render)
1514
import React.Basic.DOM as R
15+
import React.Basic.DOM.Client (createRoot, renderRoot)
1616
import React.Basic.Hooks (Component, Hook, UseEffect, UseState, coerceHook, component, useEffectOnce, useState', (/\))
1717
import React.Basic.Hooks as React
18+
import Web.DOM.NonElementParentNode (getElementById)
1819
import Web.HTML (window)
19-
import Web.HTML.HTMLDocument (body)
20-
import Web.HTML.HTMLElement (toElement)
20+
import Web.HTML.HTMLDocument (toNonElementParentNode)
2121
import Web.HTML.Window (document)
2222

2323
type Time = { hours :: Int, minutes :: Int, seconds :: Int }
2424

2525
main :: Effect Unit
2626
main = do
27-
body <- body =<< document =<< window
28-
case body of
29-
Nothing -> throw "Could not find body."
30-
Just b -> do
31-
time <- mkTime
32-
render (time {}) (toElement b)
27+
doc <- document =<< window
28+
root <- getElementById "root" $ toNonElementParentNode doc
29+
case root of
30+
Nothing -> throw "Could not find root."
31+
Just container -> do
32+
reactRoot <- createRoot container
33+
app <- mkApp
34+
renderRoot reactRoot (app {})
3335

34-
mkTime :: Component {}
35-
mkTime = do
36+
mkApp :: Component {}
37+
mkApp = do
3638
now <- JSDate.now >>= getTime
3739
component "Time" \_ -> React.do
3840
{ hours, minutes, seconds } <- useCurrentTime now
@@ -48,7 +50,7 @@ mkTime = do
4850

4951
newtype UseCurrentTime hooks = UseCurrentTime (UseEffect Unit (UseState Time hooks))
5052

51-
derive instance ntUseCurrentTime :: Newtype (UseCurrentTime hooks) _
53+
derive instance Newtype (UseCurrentTime hooks) _
5254

5355
useCurrentTime :: Time -> Hook UseCurrentTime Time
5456
useCurrentTime initialTime =

0 commit comments

Comments
 (0)