:initLocalState
is tied to the react component's lifecycle, isn't it? so it should get called every time your component is mounted, which could happen more than once for a single page render but might not necessarily
Pure react. It's part of the generated constructor
So mount/unmount
@tony you suggested the possibility of a scroll trigger a while ago.
from what I'm seeing, it looks like I'd need to create a usim/trigger
event of some kind for this. Is that right, or are there other available approaches?
I guess maybe I could attach it to a div. would that be :onScroll
?
Well, you knew the problem was that your query did not return the data you expected.... 🙂
@holyjak Hot reloading by shadow-cljs
Yes but :initLocalState
does not seem to be updated even for newly mounted components (after the hot reload), and that is the part I find weird. Since they are newly mounted, they should call the newly reloaded ctor code, shouldn't they?
Are you 100% sure that hot reload did remount them? Perhaps add the on Component Mounted hook and log from there?
Is defsc short for define stateful component?
2👍I personally use a lot of defsc to define UI root components, they usually dont have any query but I want defsc to use the fulcro css system
@wilkerlucio do you even mount those, or just use them for a place to put namespaced CSS?
yes, I mount them, they are usually the design system part of the application, buttons, checkboxes...
example case in Fulcro inspect code: https://github.com/fulcrologic/fulcro-inspect/blob/master/src/ui/fulcro/inspect/ui/core.cljs
oh, you don’t mean “root” components. I think you mean “stateless leaf” components 😉
root to me means something you pass to mount
sorry, bad naming on my part
yeah, not root
which is unfortunate, given you can also use it for stateless components, but will not change for compatibility
> The name means “define stateful component” and is intended to be used with components that have queries (though that is not a requirement). https://book.fulcrologic.com/#_the_defsc_macro
I am 100% sure that :initLocalState
is never updated once it has been set once.
That is, after modifying :initLocalState
, even new components (which are mounted only after hot reload) do not execute the new code. I have to refresh the page myself.
@ajsnow2012 I'm (slowly) grabbing them and putting them on this doc. https://roamresearch.com#/app/CommunityFulcro/page/12-26-2020
1👍well, then that would be a bug @adam678. Perhaps a regression? I’ve used that feature and do not remember such misbehavior, but perhaps I was using it in a circumstance where it didn’t matter
React HOC components in theory work fine with fulcro I think. (defsc wrapping defsc). Is there any reason to not write code this way, and if so what is the alternative that still allows me to decorate components with additional functionality?
Perhaps use defsc* to generate components dynamically that merge the queries and initial state of the wrapped and wrapper classes?
I haven't so far had a need for a HoC but I've only built two applications...
I'm trying to make a generalized "highlight on hover and tell X that you are hovered" pattern... Maybe it's just a fn wrapper? I know there are several ways to hack it together, just not sure of the best way to hack it together 🙂
Pure stateless fn wrapper
(fn wrap-highlight [factory* notify-fn*]
(fn wrap-highlight* [props]
(dom/div {:onMouseOver #(notify-fn* ...)}
(factory* props))))
But that feels wrong
Related question: is it sane to have another component render your children?
(defsc WrapperWhatever [this {:keys [:component/id] :as props} {:keys [child-props child-factory]}]
{:query [:component/id :component/local-state]
:ident :component/id}
(dom/div #_"whatever mess you want in your wrapper"
(child-factory child-props)))
(def ui-wrapper-whatever (comp/computed-factory WrapperWhatever {:keyfn :component/id}))
(defsc ParentThing [this {:keys [:parent/id :parent/child :parent/sub-wrapper] :as props}]
{:query [:parent/id
{:parent/child (comp/get-query SomeComponent)}
{:parent/sub-wrapper (comp/get-query WrapperWhatever)}]
:initial-state {:parent/child {}
:parent/sub-wrapper {}}
:ident :parent/id}
(dom/div
(ui-wrapper-whatever sub-wrapper {:child-props child :child-factory ui-some-component})))
(def ui-parent-thing (comp/factory ParentThing {:keyfn :parent/id}))
I guess this is almost the exact same question actually. Just a different approach (dare I say a more sane one?)
No time to read the code now but I believe there are examples of this in the book, perhaps using pure JS HoC
Yeah, this is about as close as I think I can come to mirroring the regular JS HOC pattern
wrapping defsc is a good idea, Wilker talks about it here: https://www.youtube.com/watch?v=R_XPwi0Kiwk
1👀1❤️