The following provide and consume Reagent components expose React contexts without fuss.
(:require [example.reagent-context :as c])
;; in a component, use `provide` to supply values for contexts:
[c/provide {:app-theme {:color "blue"}}
;; consume one context at a time
[c/consume :app-theme
(fn [theme]
[:div
{:style {:color (:color theme)}} "Colorful Text"])]]
- Contexts can be keywords or React context instances. In the case of keywords, React context instances are created behind the scenes.
- Context values are left alone, they remain as JS or Clojure values (no coercion).
- Ratoms inside
consume
work as you'd expect. - You can provide multiple contexts at the same time, but you can only
c/consume
one context at a time.
Prior art: Lokeh/reagent-context
Is this licensed for use?