reagent-project / reagent

A minimalistic ClojureScript interface to React.js
http://reagent-project.github.io/
MIT License
4.74k stars 412 forks source link

Introduce pure clojure wrappers for React Context #594

Open ingesolvoll opened 11 months ago

ingesolvoll commented 11 months ago

This PR suggests a pure Clojure API for React Context. It's a very simple wrapper around Provider and Consumer. It does not convert the context value in any way.

If the Reagent maintainers decide that this abstraction is useful and within relevant scope, I will complete this PR with tests, docs, and any other things missing in order to be considered for merge.

A short code example:

(require '[reagent.context :as r.context])

(def my-context (r.context/make-context "my-context" "default"))

[r.context/provider {:value {:foo :bar} ;; The value here can be anything, does not need to be a map
                     :context my-context}
    [r.context/consumer {:context my-context}
     (fn [{:keys [foo]}]
       [:div ":foo is a keyword: " (pr-str foo)])]

    ;; The `with-context` macro cuts away some boilerplate from the above
    (r.context/with-context [{:keys [foo]} my-context]
      [:div "From the macro: " (pr-str foo)])]

The code in this PR was written by the developers at Whimsical