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

Component-launching helper functions in React.Basic.Util #131

Closed
milesfrain opened this issue Aug 6, 2020 · 2 comments
Closed

Component-launching helper functions in React.Basic.Util #131

milesfrain opened this issue Aug 6, 2020 · 2 comments

Comments

@milesfrain
Copy link

Proposing that we add a renderInRoot helper function to make this common pattern easier:

import React.Basic.Util (renderInRoot)

myComponent :: Component {}
myComponent = do
  component "MyComponentName" \_ -> React.do
    -- ...

main :: Effect Unit 
main = renderInRoot $ myComponent {}

A slightly more verbose option is:

import React.Basic.Util (createRootElement)

mkMyComponent :: Component {}
mkMyComponent = do
  component "MyComponentName" \_ -> React.do
    -- ...

main :: Effect Unit 
main = do
  root <- createRootElement
  myComponent <- mkMyComponent
  render (myComponent {}) root

We could support both options, and reuse createRootElement in renderInRoot.

For context, Halogen has some convenient helper functions for launching components, such as awaitBody in Halogen.Aff.Util:

hookComponent = Hooks.component \_ _ -> Hooks.do
  -- ...

main :: Effect Unit
main =
  HA.runHalogenAff do
    body <- HA.awaitBody
    void $ runUI hookComponent Nothing body
@megamaddu
Copy link
Member

@milesfrain Can you move this to purescript-react-basic-dom?

@milesfrain
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants