-
Notifications
You must be signed in to change notification settings - Fork 414
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
Allowing lifecycle metadata on inner component fns #47
Comments
A similar request: |
For the record this is my current solution:
Kinda okay, except for the ugly step in accessing |
A neater solution (at the uncomfortable expense of using an internal implementation detail):
@holmsand Any chance we could de-risk this approach by making What we have above is so neat, and feels so right, compared to the
|
@mike-thompson-day8 I completely agree that I added a couple of tests for the trick (and About that name: I've been calling "functions that become components when called using An alternative/complementary approach to the "return-create-class-trick" to make sharing local state between different lifecycle methods easier is to expose the Any thoughts on that? |
I don't think that's ( |
I'd too would vote for the "return-create-class-trick" (as shown above). |
👍 return-create-class-trick Would it be possible/expensive to create classes on the fly each time one of ;; could this...
(defn my-div
[_]
(reagent/create-class
{:component-did-mount #(println "main: component-DID-mount")
:component-will-mount #(println "main: component-WILL-mount")
:component-function (fn [_]
[:div {:style {:border "1px solid black"}}
"some div"])}))
;; become this?
(defn my-div [_]
[:div {:style {:border "1px solid black"}
:component-did-mount #(println "main: component-DID-mount")
:component-will-mount #(println "main: component-WILL-mount")}
"Some div..."])
|
@Frozenlock I'm not keen to see |
Maybe it's because I've used the metadata approach up until now, but I've always found the lifecycle functions to be a hassle. At least in comparison to everything else which pretty much just works. I think of it this way: what if the attributes were metadata up until now? (defn my-div [a]
[:div (str "my div :" a)])
(with-meta
my-div {:style {:border "1px solid black"}}) ;; boooo!
;;; but hiccup had the brilliant idea of an optional
;;; attributes (properties) map
(defn my-div [a]
[:div {:style {:border "1px solid black"}}
(str "my div :" a)]) ;; yeah! Anyways, I don't use the lifecycle functions enough to have any strong feelings about it. |
I also find that |
I've created a Wiki page, so we have an explanatory resource to point to when this issue comes up again: https://github.com/reagent-project/reagent/wiki/Creating-Components |
Great wiki page @mike-thompson-day8 but it makes me wonder whether adding In fact, why not use |
Doesn't How about Or |
👍 |
Well, anything with
Because, |
I'm suggesting mapping from "Reagent keys" which we choose and control to "React keys" rather than just slavishly following the React lifecycle names as keys. As noted on @mike-thompson-day8 wiki page: "Its a trap to mistakenly use I think we can remove that mistake by making Having to repeatedly type |
@skrat and @seancorfield Naming is hard :) I agree that As you say @seancorfield, switching to something like In the meantime, the-thing-that-is-now-component-function must be called something, and since there seems to be universal hate for the previous alternatives, how about these:
That would maybe be a temporary solution anyway, if we go with |
+1 for |
Reagent 0.5.0-alpha3 now supports |
I think this issue can be closed |
I kinda like the |
Actually, I believe this should still work if you wrap the inner component with a vector: (fn outer-component [arg1 arg2]
(let [local-state (atom {})] ; Perform setup, etc.
[(with-meta
(fn inner-component [arg1 arg2]
[:div (str arg1 "," arg2 "," @local-state)])
{:component-did-mount
(fn [this]
;; Has access to local-state (but doesn't currently trigger)
)})])) |
I have try your answer, actually, pass (fn outer-component [arg1 arg2]
(let [local-state (atom {})] ; Perform setup, etc.
[(with-meta
(fn inner-component []
[:div (str arg1 "," arg2 "," @local-state)])
{:component-did-mount
(fn [this]
;; Has access to local-state (but doesn't currently trigger)
)})])) But this is against with |
Hi Dan, hope you're well!
Think this might be clearer to explain with an example. It's currently possible to write something like:
This works, but the :component-did-mount fn has to do w/o access to
local-state
.It's be nice to support something like this:
I.e. by allowing lifecycle metadata on the inner component, we gain the ability to share local setup state with the mount fn.
Does that make sense?
Think it'd be feasible?
Any downsides?
Would a PR be welcome?
Thanks a lot! No urgency on this at all, know you've been busy.
Cheers :-)
The text was updated successfully, but these errors were encountered: