This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-07-03
Channels
- # announcements (13)
- # babashka (1)
- # beginners (4)
- # calva (13)
- # clojure (13)
- # clojure-europe (12)
- # clojure-norway (2)
- # community-development (7)
- # conjure (9)
- # core-typed (5)
- # cursive (5)
- # data-science (15)
- # datomic (2)
- # fulcro (10)
- # hyperfiddle (3)
- # leiningen (1)
- # missionary (65)
- # nbb (6)
- # off-topic (3)
- # shadow-cljs (11)
- # specter (3)
- # vim (8)
@tony.kay I have written about how to use forwardRef
with Fulcro https://github.com/holyjak/blog.jakubholy.net/blob/master/content/asc/posts/2022/forward-ref-in-fulcro.asc#using-forwardref-to-access-ref-passed-by-a-parent and would very much appreciate your feedback, especially with the regard of the necessary conversion from #js props to a clj map. Perhaps there is a simpler way that I am missing? I would then also like to create a PR to add this to a suitable place of the F. Dev. Guide, likely in the Hooks chapter as it took some effort to figure this out.
I’m not sure how this is working:
(defsc ChildWithRef [_ {:keys [forwarded-ref label] :as props}] ; (1)
{}
(dom/button {:onClick #(some-> forwarded-ref .-current .focus)}
label))
(def ui-child-with-ref (comp/factory ChildWithRef))
(def child-with-ref
(react/forwardRef ; (2)
(fn [js-props ref]
(ui-child-with-ref ; (3)
(-> js-props
(js->clj :keywordize-keys true) ; (4)
(assoc :forwarded-ref ref))))))
(defsc Root [_ _]
{:use-hooks? true}
(let [^:js ref (hooks/use-ref nil)]
(div
(dom/input {:ref ref, :value "" :type "text"})
((interop/react-factory child-with-ref) ; (5)
{:ref ref :label "Focus, v3!"})))) ; (6)
because you’re using defsc
, and then you’re messing with the low-level js props…but Fulcro’s props are actually in the js props at fulcro$value
(I think…or is it state). Anyway, when you destructure forwarded-ref you’re pulling from that and NOT the low-level js props.Yes I tested it but feel there must be better way. I also realized later it does not cover the full use case, where my Fulcro component will be passed to a non-fulcro component and thus I likely need with-parent-context
.
If you want to pass a Fulcro component AS the HOC, then I’d write a low-level function and make a factory for it (i.e. make a js hooks component), and do the “adapting” in that function. Then React is none the wiser. The new hooks use-component
stuff makes that much easier to use when doing composition.
@tony.kay thanks a lot for that tip. I have applied it here https://gist.github.com/holyjak/ff152bb5c0edaf9575e0ba2b051e8a8a and it works perfectly. Anything you would change there?
A simplified example of using forwardRef while passing the component to a HoC JS component (thus needed with-parent-context), leveraging hooks/with-component here: https://github.com/holyjak/blog.jakubholy.net/blob/master/content/asc/posts/2022/forward-ref-in-fulcro.asc#passing-a-fulcro-component-wrapped-with-forwardref-to-a-hoc-js-component