Fork me on GitHub
#helix
<
2020-06-11
>
fabrao18:06:37

How do I include/inject a prop into helix component?

lilactown18:06:26

what do you mean?

Aron18:06:46

probably &

fabrao18:06:09

If I have a prop that I don´t want to include in all my components like ($ Comp {:inject value} children)

fabrao18:06:13

I thought do something like this:

(ns app.lib
  #?(:clj (:require [helix.core :as helix]))
  #?(:cljs (:require-macros [app.lib])))

#?(:clj
   (defmacro defnc [type params & body]
     (let [opts? (map? (first body)) ;; whether an opts map was passed in
           opts (if opts?
                  (first body)
                  {})
           body (if opts?
                  (rest body)
                  body)
           ;; feature flags to enable by default
           default-opts {:helix/features {:fast-refresh true}}]
       `(helix.core/defnc ~type ~params
          ;; we use `merge` here to allow indidivual consumers to override feature
          ;; flags in special cases
          ~(merge default-opts opts)
          ~@body))))

fabrao18:06:31

like in default-opts, is that right?

lilactown18:06:05

in all of your components?

lilactown18:06:33

does the value change?

lilactown18:06:55

I would use React context for this

lilactown18:06:20

and when you need the value, you can use use-context to get it

lilactown18:06:20

(def value-context (helix/create-context "some value"))
then in your components:
(defnc my-component [props]
  (let [value (hooks/use-context value-context)]
   ,,,))

fabrao18:06:08

Understand, thanks

fabrao18:06:01

so, you shoud use use-context too, 🙂 just kidding