Fork me on GitHub
#reagent
<
2022-04-19
>
pmooser12:04:46

I'm trying to follow some MUI examples, and I'm not sure how to translate this to clojurescript/reagent:

function ValueLabelComponent(props: Props) {
  const { children, value } = props;

  return (
    <Tooltip enterTouchDelay={0} placement="left" title={value}>
      {children}
    </Tooltip>
  );
}
Any pointers?

vanelsas12:04:15

There is pretty good documentation available at the Reagent github repo, including examples on how to deal with props. Maybe that can help? https://github.com/reagent-project/reagent/blob/master/doc/InteropWithReact.md

pmooser12:04:25

Yes, I'm very familiar with that page. I personally think all the docs are written as if we are all react experts, which sadly I am not. That's why I find names like argv for functions that deal with props to be rather obscure and not particularly illuminating.

pmooser12:04:35

It seems like this almost works:

pmooser12:04:55

(defn value-label-component
  [props]
  (let [value (:value props)
        children (:children props)]
    [mui-tooltip/tooltip {:placement "bottom" :title value}
     children]))

pmooser12:04:09

And I think if I reactify-component that, it may work!

vanelsas12:04:33

I can't test it where I am now. Looks like you may be missing a (reagent/current-component) to access the props and children. I remember an article I read on this once, let me see if I can find it

pmooser12:04:13

Ok, thank you very much for your help! I will read that, and I bet it will help me!

Prometheus11:04:51

(:require ["@mui/material" :refer [Tooltip]]) ; inyour ns

(def tooltip (reagent/adapt-react-class Tooltip))

Then you can just use tooltip as a reagent function would

In your case it will be:

(defn value-label-component [{:keys [children value]}]
   [tooltip {:enter-touch-delay 0 :placement "left" :title value} children])

erwinrooijakkers11:04:46

You can also use the :> syntax as a shorthand for adapt-react-class:

(:require 
  ["@mui/material" :refer [Tooltip]] ;; or maybe :default Tooltip (without []) instead of :refer [Tooltip]

(defn value-label-component [{:keys [children value]}]
   [:> Tooltip {:enter-touch-delay 0 :placement "left" :title value} children])
The above require is with using shadow-cljs and npm. Also see https://github.com/reagent-project/reagent/blob/master/doc/ReactFeatures.md

👍 1
Prometheus11:04:57

Yeah, I think there are good arguments for both approaches, I think when using a library like material having the tags in more uniform syntax to reagent makes sense because it will be almost everywhere and also needs to be reusable. I often create a separate namespace where I adapt all the external react components I need

👍 1
Prometheus11:04:47

It is also how I approach making react native apps, I just copy all the adapt react classes from a previous project and reuse them. I think this is a nicer approach than using or creating a wrapper library because it gives you more control over what you import and how it is used.

pmooser12:04:20

I'm afraid that despite having used reagent for a while, I always get a bit confused with react props.