Fork me on GitHub
Mr. Savy03:12:39

@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?

👍 6

which is unfortunate, given you can also use it for stateless components, but will not change for compatibility

Björn Ebbinghaus13:12:40

> The name means “define stateful component” and is intended to be used with components that have queries (though that is not a requirement).


Right, it is a little bit of a misnomer, but it is really hard to come up with good names. The vast majority of components you write in Fulcro will have queries, and therefore state in the database. Technically pre-hooks such components always have access to component-local state, so you can say that the name is actually completely accurate for the time period in which it was named. If you don’t need any kind of state, then what you want is a function. However, now that hooks are here it is possible you might want to define a component that is just a function (presumably for side effects), and while there is usually some kind of state involved it is technically possible to make a thing that has no concept of state…but using defsc for hooks also implies that you are going to use a query for that case…otherwise why not just write a function?


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


@U066U8JQJ 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...


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


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?

Jakub Holý (HolyJak)20:12:23

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


wrapping defsc is a good idea, Wilker talks about it here:

👀 3
❤️ 3

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}
    (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?)

Jakub Holý (HolyJak)20:12:41

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


Yes @U5P29DSUS, that’s about the idea. This is a case where the ui tree can vary slightly in shape from the query (them matching exactly is a simplification for new user understanding)


Cool, that works.


you also have access to comp/children, so you can get the notation of:

(ui-wrapper-whatever sub-wrapper
  (ui-some-component child))


non-computed factories send extra things as children