: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
?
Is defsc short for define stateful component?
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
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