fulcro

Book: http://book.fulcrologic.com, Community Resources: https://fulcro-community.github.io/, RAD book at http://book.fulcrologic.com/RAD.html
Timofey Sitnikov 2021-02-05T12:31:27.249400Z

Good morning all, I have been programmed to think of this as the pointer to the current object and I keep getting tripped up with the use of this in clojure. For example in the Fulcro RAD Demo these lines of code:

(defsc LandingPage [this props]
   {:query         ['*]
    :ident         (fn [] [:component/id ::LandingPage])
    :initial-state {}
    :route-segment ["landing-page"]}
      (dom/div "Welcome to the Demo. Please log in."))
The first element is of the vector the this , what is the right way to read it? I feel like I missed something somewhere.

Timofey Sitnikov 2021-02-06T13:38:46.254400Z

@posobin, yeh, that was something that tripped me in the beginning, I even searched for something special that is called this in Clojure, but from Tony's comments I understood that it is just the variable that carries the instance of the components.

đź‘Ť 2
2021-02-05T13:04:21.251400Z

this is the instance of the component being rendered (rather than the component's class).

tony.kay 2021-02-05T14:20:07.251500Z

It is “abstractly” an instance of that class (for Fulcro’s APIs). Concretely in render it is the this described by https://reactjs.org/docs/react-component.html#render

tony.kay 2021-02-05T14:21:51.251800Z

But since Fulcro hacks CLJ support on top of the js in render, you should generally treat it as a pure abstract representation of the on-screen instance, and not a concrete OO instance of a class. In fact, when using hooks, there is no class.

1
tony.kay 2021-02-05T14:22:16.252Z

That said, for any React API that needs the this of render, that is what you need.

Timofey Sitnikov 2021-02-05T14:58:46.252200Z

OK, so the vector [this props] seems to be a common interface. I think I get it little better, but will need to look through it more to understand its content.

tony.kay 2021-02-05T19:15:45.253200Z

yeah…the props is because I actually put Fulcro props (which are CLJS data) in a special place on the react props….so the props you get “look like” what you expect, but are in fact just one element of the real js props from React.

tony.kay 2021-02-05T19:16:07.253400Z

That way you get full CLJS experience without having to use js interop.

tony.kay 2021-02-05T19:16:24.253600Z

you can always get raw React props with (.-props this)

tony.kay 2021-02-05T19:17:01.253800Z

The macro just does all that destructuring and lookup for you so you can concentrate on the body of render, and not the details of how things get there

Timofey Sitnikov 2021-02-05T19:25:55.254Z

That makes sense, thank you.

Gleb Posobin 2021-02-05T22:58:52.254200Z

JIC, this can be replaced with any other variable name, there is nothing special about that particular word.