-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React createContext
wrapper
#19
Comments
I don't see why not - PRs welcome! I might get around to this during the weekend. In the meantime I'm going to try and clean up the code so that it's easier for others to jump into 😅 |
Hey I'd like to help with this. Any feedback would be helpful because I'm not very familiar with createContext. I'm thinking about adding the wrapper function that returns a map containing the producer and consumer: (defn context
"Use react/createContext and return a map of Producer and Consumer."
[value]
(let [context (react/createContext value)]
{:provider (.-Provider context)
:consumer (.-Consumer context)})) However, I'm not sure how to test this function. Would you give me some suggestion? Thank you! |
I just found the function |
Hmm good point about One thing to note is that most libraries expose So the case we're talking about is most useful when we're using a Context value we've created ourselves. Another option: we could add a new hiccup element, Example: (def my-context (react/createContext))
(defnc Child [_]
(let [value (<-consume my-context)]
[:div value]))
(defnc App [_]
[:provide {:context my-context
:value "something"}
[Child]]) I can think of other ways, like trying to get crafty with protocols or whatnot, but probably not worth the effort. |
Thanks for responding @Lokeh !
I'm not sure if I understand you correctly. Do you have an example that exposes Provider and Consumer separately? I'm just not sure which would be more valuable/desirable: a) expose raw Context, or b) expose Provider and Consumer, or c) all of them. (Sorry I don't have much experience with the context object.) I like your example above. It feels a lot like what I'd rather write in clojure instead of thinking the raw js objects. Do you think this is a separate PR? |
Sure, an example would be the way the Emotion CSS library does theming: https://github.com/emotion-js/emotion/blob/master/docs/theming.md They expose a In this case we don't need to do anything fancy, we can just use the The annoying bit is when, in CLJS, we want to create and use context. Right now, you have to use JS interop to get at the This isn't super heinous to me but I see why people might not like it. So I think that to aid with this, we should have a We could also have a At the end of the day this is all small QoL enhancements so not super high on my list. But PRs welcome! |
Thanks, I see how this is useful now. I would love to work on the PR for this. Thanks for taking the time explaining. |
Closing this with #25 merged |
Are there any plans to add wrappers for things such as
createContext
(similar to the dummy wrappers inhx.hooks
such as<-reducer
)?It would be nice to do some Clojure wrapping to for easily getting out the
Consumer
andProvider
by key.The text was updated successfully, but these errors were encountered: