Skip to content

Commit 2c32f99

Browse files
committed
Update TicTacToeReactHooks
1 parent 2ad1135 commit 2c32f99

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

recipes/TicTacToeReactHooks/spago.dhall

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

recipes/TicTacToeReactHooks/src/Main.purs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,27 @@ import Data.Maybe (Maybe(..), fromMaybe, isJust)
1515
import Data.Show.Generic (genericShow)
1616
import Effect (Effect)
1717
import Effect.Exception (throw)
18-
import React.Basic.DOM (CSS, css, render)
18+
import React.Basic.DOM (CSS, css)
1919
import React.Basic.DOM as R
20+
import React.Basic.DOM.Client (createRoot, renderRoot)
2021
import React.Basic.Events (EventHandler, handler_)
2122
import React.Basic.Hooks (Component, JSX, Reducer, component, keyed, mkReducer, useReducer, (/\))
2223
import React.Basic.Hooks as React
24+
import Web.DOM.NonElementParentNode (getElementById)
2325
import Web.HTML (window)
24-
import Web.HTML.HTMLDocument (body)
25-
import Web.HTML.HTMLElement (toElement)
26+
import Web.HTML.HTMLDocument (toNonElementParentNode)
2627
import Web.HTML.Window (document)
2728

2829
main :: Effect Unit
2930
main = do
30-
body <- body =<< document =<< window
31-
case body of
32-
Nothing -> throw "Could not find body."
33-
Just b -> do
34-
game <- mkGame
35-
render (game unit) (toElement b)
36-
mempty
31+
doc <- document =<< window
32+
root <- getElementById "root" $ toNonElementParentNode doc
33+
case root of
34+
Nothing -> throw "Could not find root."
35+
Just container -> do
36+
reactRoot <- createRoot container
37+
app <- mkApp
38+
renderRoot reactRoot (app {})
3739

3840
mkSquare :: Component { onClick :: EventHandler, value :: Square }
3941
mkSquare =
@@ -71,8 +73,7 @@ mkBoard
7173
mkBoard = do
7274
square <- mkSquare
7375
component "Board" \props -> React.do
74-
let
75-
renderSquare i j = square { value: props.squares i j, onClick: props.onClick i j }
76+
let renderSquare i j = square { value: props.squares i j, onClick: props.onClick i j }
7677
pure
7778
$ R.div_
7879
[ R.div
@@ -142,8 +143,8 @@ initialState =
142143
, xIsNext: true
143144
}
144145

145-
mkGame :: Component Unit
146-
mkGame = do
146+
mkApp :: Component {}
147+
mkApp = do
147148
board <- mkBoard
148149
reducer <- reducerFn
149150
component "Game" \_ -> React.do
@@ -168,7 +169,9 @@ mkGame = do
168169
moves :: Array JSX
169170
moves =
170171
renderMove "Go to start" 0
171-
: Array.mapWithIndex (\i _ -> renderMove ("Go to move #" <> show (i + 1)) (i + 1)) (NonEmpty.tail state.history)
172+
: Array.mapWithIndex
173+
(\i _ -> renderMove ("Go to move #" <> show (i + 1)) (i + 1))
174+
(NonEmpty.tail state.history)
172175

173176
status :: String
174177
status = case calculateWinner current of
@@ -222,13 +225,7 @@ calculateWinner f =
222225

223226
-- These styles could be provided by a proper stylesheet, we are only
224227
-- defining them here for the sake of compatibility with TryPureScript
225-
styles
226-
:: { list :: CSS
227-
, boardRow :: CSS
228-
, square :: CSS
229-
, game :: CSS
230-
, gameInfo :: CSS
231-
}
228+
styles :: { list :: CSS, boardRow :: CSS, square :: CSS, game :: CSS, gameInfo :: CSS }
232229
styles =
233230
{ list:
234231
css

0 commit comments

Comments
 (0)