This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-03-25
Channels
- # babashka (77)
- # beginners (107)
- # calva (20)
- # cider (2)
- # clj-kondo (7)
- # clojure (63)
- # clojure-australia (2)
- # clojure-europe (75)
- # clojure-germany (10)
- # clojure-italy (3)
- # clojure-nl (3)
- # clojure-serbia (15)
- # clojure-spain (2)
- # clojure-uk (24)
- # clojurescript (54)
- # clojureverse-ops (3)
- # cursive (20)
- # datahike (4)
- # datalog (5)
- # datascript (8)
- # datomic (13)
- # emacs (2)
- # fulcro (1)
- # graalvm (2)
- # instaparse (1)
- # jobs (2)
- # luminus (1)
- # malli (7)
- # off-topic (28)
- # pathom (6)
- # pedestal (2)
- # re-frame (5)
- # reagent (9)
- # remote-jobs (4)
- # rewrite-clj (4)
- # ring (19)
- # shadow-cljs (2)
- # spacemacs (2)
- # sql (10)
- # tools-deps (6)
- # xtdb (12)
I'd like to use bindings to control how certain components are rendered, but it's not working (because Reagent doesn't render 'inner' elements (e.g. [foo]
) until after the original scope has been exited). Is there a way around this?
this is really a function of how React works. component's render functions are not called until later
you can try something like:
(def my-context (react/createContext {:some-value "default"}))
(def my-context-provider (.-Provider my-context))
(def my-context-consumer (.-Consumer my-context))
(defn component-that-uses-context []
[:> my-context-consumer
(fn [{:keys [some-value]}]
(r/as-element [:div "some-value is: " some-value])]))
(defn component-that-provides-context []
[:> my-context-provider {:value {:some-value "not default"}}
[component-that-users-context]])
but watch out for this gotcha: https://github.com/reagent-project/reagent/issues/371
useContext
Hook can be useful, just remember to use this component with :f>
:
(defn component-that-uses-context []
(let [{:keys [some-value]} (react/useContext my-context)]
[:div "some-value is: " some-value]))
(defn foo []
[context-provider
[:f> component-that-uses-contect]])
Bindings are also impossible to use, as it is not certain that the parent render has been called when a element render is called. If ratoms or such triggers the re-render, only that element and its children are re-rendered.
cool, thanks!
I'll only want to do this in a few places, so I'm fine with the solution being a bit messy 🙂