Fork me on GitHub
#reagent
<
2020-03-25
>
tatut09:03:28

Is there a convenient way to use react context? I got something working by React.createContextand adapting the Provider and Consumer classes in the returned object

tatut09:03:56

but the context value will be turned into js and it's not very convenient to use

tatut09:03:34

if there was a way to wrap something around the reagent rendering, you could use dynamic variables for context...

David Pham10:03:00

I am speaking for myself: not always feasible but I found re-frame, event/subscription model easy to use to simulate the context

juhoteperi11:03:25

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.

juhoteperi12:03:55

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

juhoteperi12:03:35

(r/create-element Consumer {:clj :map ...} ...) should keep the properties object as clj map

👍 4
juhoteperi12:03:13

If that helps, I can update the docs to mention this.

juhoteperi12:03:11

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 :>

tatut15:03:06

that worked well, with a minor wrapper to autocreate context by keyword name, the API is pretty simple