Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 21 react examples to v18 #298

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update GroceriesReactHooks
  • Loading branch information
andys8 committed Aug 13, 2022
commit 3016c3f4e9477397522e979fd4ff0f84e66d232e
1 change: 1 addition & 0 deletions recipes/GroceriesReactHooks/spago.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
, "prelude"
, "react-basic-dom"
, "react-basic-hooks"
, "web-dom"
, "web-html"
]
, packages = ../../packages.dhall
Expand Down
56 changes: 29 additions & 27 deletions recipes/GroceriesReactHooks/src/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,39 @@ import Prelude
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Exception (throw)
import React.Basic.DOM (div_, h1_, li_, render, text, ul_)
import React.Basic.DOM (div_, h1_, li_, text, ul_)
import React.Basic.DOM.Client (createRoot, renderRoot)
import React.Basic.Hooks (Component, component)
import Web.DOM.NonElementParentNode (getElementById)
import Web.HTML (window)
import Web.HTML.HTMLDocument (body)
import Web.HTML.HTMLElement (toElement)
import Web.HTML.HTMLDocument (toNonElementParentNode)
import Web.HTML.Window (document)

main :: Effect Unit
main = do
body <- body =<< document =<< window
case body of
Nothing -> throw "Root element not found."
Just b -> do
groceries <- mkGroceries
render (groceries {}) (toElement b)
doc <- document =<< window
root <- getElementById "root" $ toNonElementParentNode doc
case root of
Nothing -> throw "Could not find root."
Just container -> do
reactRoot <- createRoot container
app <- mkApp
renderRoot reactRoot (app {})

mkGroceries :: Component {}
mkGroceries = do
component "Groceries" \_ -> React.do
pure
$ div_
[ h1_ [ text "My Grocery List" ]
, ul_
[ li_ [ text "Black Beans" ]
, li_ [ text "Limes" ]
, li_ [ text "Greek Yogurt" ]
, li_ [ text "Cilantro" ]
, li_ [ text "Honey" ]
, li_ [ text "Sweet Potatoes" ]
, li_ [ text "Cumin" ]
, li_ [ text "Chili Powder" ]
, li_ [ text "Quinoa" ]
]
]
mkApp :: Component {}
mkApp = component "Groceries" \_ -> React.do
pure
$ div_
[ h1_ [ text "My Grocery List" ]
, ul_
[ li_ [ text "Black Beans" ]
, li_ [ text "Limes" ]
, li_ [ text "Greek Yogurt" ]
, li_ [ text "Cilantro" ]
, li_ [ text "Honey" ]
, li_ [ text "Sweet Potatoes" ]
, li_ [ text "Cumin" ]
, li_ [ text "Chili Powder" ]
, li_ [ text "Quinoa" ]
]
]