purescript-contrib / purescript-react-dom

Low-level React DOM bindings for PureScript
MIT License
16 stars 16 forks source link

createRoot (replacing render) #29

Open andys8 opened 2 years ago

andys8 commented 2 years ago

React 18 was released. It encourages a switch from render to createRoot to benefit from the new implementation.

andys8 commented 2 years ago

There is this (different lib): https://pursuit.purescript.org/packages/purescript-react-basic-dom/4.2.0/docs/React.Basic.DOM.Concurrent#v:createRoot

But results in image

andys8 commented 2 years ago

Cross link: https://github.com/lumihq/purescript-react-basic-dom/issues/42#issue-1289387789

tellnobody1 commented 1 year ago
"use strict"

import ReactDOMClient from "react-dom/client"

export const createRoot = container => () => ReactDOMClient.createRoot(container)
export const render = root => reactElement => () => root.render(reactElement)
import Effect (Effect)
import Prelude
import React (ReactElement)
import Web.DOM.Element (Element)

foreign import data Root :: Type
foreign import createRoot :: Element -> Effect Root
foreign import render :: Root -> ReactElement -> Effect Unit
import Data.Maybe (fromJust)
import Partial.Unsafe (unsafePartial)
import React (createLeafElement)
import Web.DOM.NonElementParentNode (getElementById)
import Web.HTML (window)
import Web.HTML.HTMLDocument (toNonElementParentNode)
import Web.HTML.Window (document)

main :: Effect Unit
main = do
  doc <- window >>= document
  container <- getElementById "container" $ toNonElementParentNode doc
  root <- createRoot $ unsafePartial $ fromJust container
  render root $ createLeafElement appClass props
<div id="container"></div>
<script src="index.js"></script>