Is there a convenient way to use react context? I got something working by React.createContext
and adapting the Provider and Consumer classes in the returned object
but the context value will be turned into js and it's not very convenient to use
if there was a way to wrap something around the reagent rendering, you could use dynamic variables for context...
I am speaking for myself: not always feasible but I found re-frame, event/subscription model easy to use to simulate the context
Dynamic variables are problematic for Reagent rendering, as render happens asynchronously at some point. Reagent implementation could wrap the render bodies in a call that sets the dynamic properties always, but another (bigger maybe) problem is that if a node is rendered, its parent aren't necessarily rendered, so dynamic vars from those wouldn't get set. But this might be related to ideas I have around controlling rendering more.
Docs have a very simple example for using React.createContext
and Provider/Consumer, but it's probably same as what @tatut is already doing. Not sure what the problem is, but to workaround default properties conversion, using r/create-element
might help
(r/create-element Consumer {:clj :map ...} ...)
should keep the properties object as clj map
If that helps, I can update the docs to mention this.
Maybe all the docs should be rewritten to first recommend using r/create-element
for interop, and then say, if you want to automatically convert clj properties to JS object, use adapt-react-class
or :>
that worked well, with a minor wrapper to autocreate context by keyword name, the API is pretty simple