-
Notifications
You must be signed in to change notification settings - Fork 30
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
Compact and warning-free React recipes #198
Comments
I think it would be better to get this utility function merged and then update the cookbook here.
It sounds like you are proposing a "shared" folder that contains code that all recipes can utilize if desired. If so, I'm against for two reasons. First, it could break the principle of "if it breaks the build, just move it into the 'broken' folder." If there is an issue in the shared code, it will affect all recipes. Second, it makes recipes less stand-alone where one can copy it and adjust things from there. |
Yes, the plan is to get the utility function into React first. Not proposing shared cookbook code outside of recipes, but rather rewriting some of the react recipe code so that its identical between recipes. So for example, remove "button" mentions from: main :: Effect Unit
main = do
body <- body =<< document =<< window
case body of
Nothing -> throw "Could not find body."
Just b -> do
buttons <- mkButtons
render (buttons {}) (toElement b)
mkButtons :: Component {}
mkButtons = do
component "Buttons" \_ -> React.do
-- ... To get: main :: Effect Unit
main = do
body <- body =<< document =<< window
case body of
Nothing -> throw "Could not find body."
Just b -> do
myComponent <- mkMyComponent
render (myComponent {}) (toElement b)
mkMyComponent :: Component {}
mkMyComponent = do
component "MyComponent" \_ -> React.do
-- ... This makes batch changes easier. #197 was a bit tedious with preserving these names. |
Oh, ok. Yeah, go ahead and make that change!
|
My assumption is that most people will just have a @JordanMartinez Is the idea still to push for a helper in |
I'd prefer it be fixed upstream. |
Hm, I'm not sure if (and when) the proposed helper function will make it into react basic. I doubt that it'll assume the existence of a So what I'm thinking of is updating the current examples to render into a When I've time I can give it a try. No sure if there are any blockers. Update: I figured #291 is the current blocker. diff --git a/recipes/HelloReactHooks/spago.dhall b/recipes/HelloReactHooks/spago.dhall
index 0046986..f1f0ea7 100644
--- a/recipes/HelloReactHooks/spago.dhall
+++ b/recipes/HelloReactHooks/spago.dhall
@@ -9,6 +9,7 @@
, "react-basic-dom"
, "react-basic-hooks"
, "web-html"
+ , "web-dom"
]
, packages = ../../packages.dhall
, sources = [ "recipes/HelloReactHooks/src/**/*.purs" ]
diff --git a/recipes/HelloReactHooks/src/Main.purs b/recipes/HelloReactHooks/src/Main.purs
index ab5713f..e450ce0 100644
--- a/recipes/HelloReactHooks/src/Main.purs
+++ b/recipes/HelloReactHooks/src/Main.purs
@@ -1,25 +1,26 @@
module HelloReactHooks.Main where
import Prelude
+
import Data.Maybe (Maybe(..))
import Effect (Effect)
import Effect.Exception (throw)
import React.Basic.DOM (render)
import React.Basic.DOM as R
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 "Could not find body."
+ root <- getElementById "root" <<< toNonElementParentNode =<< document =<< window
+ case root of
+ Nothing -> throw "Could not find root."
Just b -> do
helloComponent <- mkHelloComponent
- render (helloComponent {}) (toElement b)
+ render (helloComponent {}) b
mkHelloComponent :: Component {}
mkHelloComponent = do |
Has this even been discussed in that repo yet? |
Just saw the link towards the start of this issue. Given that that's been open for a while, let's move forward with adding the helper function in each recipe. If |
I'm noticing there's a Edit: Ah, I'm realizing we'd also have to update all the |
I'd be fine with that. |
All The snippet in the first function instead creates a element and appends it to the body. Since this repository can be taken as a starting point, I would try to keep the examples similar to what the most common way to initialize an application is. And it looks to me like this would use an element in the body and render into it (instead of So, if there is no good reason to do it differently, I would recommend sticking to the initialization similar to how react docs suggest it. Update: I'm wondering if this issue is more about React docs
https://reactjs.org/docs/add-react-to-a-website.html#step-1-add-a-dom-container-to-the-html Create react app
npx create-react-app my-app
ack "id=\"root\""
public/index.html
31: <div id="root"></div> |
Yeah I think the issue with assuming |
I think if we can use the |
I'd be good with the Try PureScript change. That would simplify things and still make the code readable. |
Just to make sure I'm understanding, do you mean
I don't really have a preference either way. As @andys8 noted, using |
Ah, sorry for the ambiguity. I think pushing for 1 is desirable as I suspect that Try PureScript may be using |
The |
Ah. Okay. But both This is probably going a bit too far, but on first glance it looks like I would first get it working and then we can play around with combination of logging and rendering, and see if further changes would improve it :) |
Closed by #294 |
Oh, wait, the other React recipes still need to be updated. But, this is now unblocked. |
|
Follow-up to #197
The existing code produces a warning in the dev console with this strategy:
Here's a warning-free option that's a little more verbose:
It would be most ideal to have a utility function that handles this common pattern:
Tracking a proposal to add this utility function to the React library in purescript-react/purescript-react-basic-dom#8
Also wondering if we should make code that's reused between recipes more generic (even if
scripts/newRecipe.sh
) handles this renaming for us. For example, removing "buttons" where possible:The text was updated successfully, but these errors were encountered: