Fork me on GitHub
#fulcro
<
2021-02-05
>
Timofey Sitnikov12:02:27

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.

tony.kay14:02:07

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.kay14:02:51

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.

3
tony.kay14:02:16

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

Timofey Sitnikov14:02:46

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.kay19:02:45

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.kay19:02:07

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

tony.kay19:02:24

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

tony.kay19:02:01

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 Sitnikov19:02:55

That makes sense, thank you.

Gleb Posobin22:02:52

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

Timofey Sitnikov13:02:46

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

👍 6
codonnell13:02:21

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