Fork me on GitHub
#fulcro
<
2018-11-27
>
codxse07:11:58

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>]
 my-options
 (dom/div {:onClick (:onClick computed)} (:db/id props)))

claudiu08:11:22

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

codxse10:11:32

sure thanks, :))

claudiu10:11:24

@U0C7HNARK Welcome. By "nice validations" I mean compile-time stuff, that's really useful ex: https://gist.github.com/claudiuapetrei/77043c6c18e6861681be59109c4a7b6b

codxse16:11:44

I ended up wite my own macro.

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

tony.kay16:11:58

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

tony.kay16:11:25

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

tony.kay16:11:01

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

tony.kay16:11:25

It just isn’t failing to compile 😜

tony.kay16:11:20

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.

codxse16:11:16

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
tony.kay16:11:40

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

👍 1
exit219:11:01

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

tony.kay22:11:43

@njj that is pure react

tony.kay22:11:35

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.

tony.kay22:11:02

but in general DOM rendering is under complete control of React