Fork me on GitHub

Hallo, I'm trying to understand defsc macro. The defsc look like this:

(defsc [this props <optional-computed> <optional-css-map-if-using-css>]
 { ...options... }
 (dom/div {:onClick (:onClick computed)} (:db/id props)))
The { ...options... } is just a map right? it's a data. But, why I cant treat { ...options... } map as clojure data. I mean the code below doesn't works:
(def my-options  { ...options... })

(defsc [this props <optional-computed> <optional-css-map-if-using-css>]
 (dom/div {:onClick (:onClick computed)} (:db/id props)))


@U0C7HNARK defsc is a macro. It does a lot of really nice things behind the scenes including some validations. Guess that is the trade-off with macros in general, it needs literals not variables.


sure thanks, :))


@U0C7HNARK Welcome. By "nice validations" I mean compile-time stuff, that's really useful ex:


I ended up wite my own macro.

(defmacro defcomponent
     [compname params opts & body]
     `(prim/defsc ~compname ~params
        ~(or opts {})
        [email protected]))
It does work.


@U0C7HNARK You cannot do that because I prefer error-checking the data over this particular use-case (which isn’t even that useful), and there is no reliable way to expand an expression at compile time.


For example, if you destructure the wrong things (you have not queried for) you’ll get useful error messages instead of runtime bad behavior


Your macro does nothing useful I can see…the defsc is already ok with an optional opts. Your hack is just making the macro think that you didn’t pass options. It is certainly broken


It just isn’t failing to compile 😜


The macro you wrote is making defsc think that the code body has an extra or expression…it is definitely not using them as the proper options.


I see your point. It's hard problem to solve checking expression at compile time. In my own use case, I have to do something with opts. Not so much, but little tricky. And yes opts & body is just wrong :rolling_on_the_floor_laughing:. Btw, thank you for the feedback and all the hard work. It's such a nice community.

👍 1

anything you have to do with opts needs to run in a macro, and emit a literal map, not a symbol or expression

👍 1

Is there a Fulcro method for checking if a dom element has been rendered?


@njj that is pure react


e.g. you could put a :ref on it, and the function you give will be called with the DOM element, indicating it has been “initially rendered”…if the ref changes, then it’ll be called with nil and then the DOM element.


but in general DOM rendering is under complete control of React