After a while of digging I found the solution to be a combination of using 
react's `initLocalState` lifecycle method and `om.next/get-state` as hinted 
to in Chris' SO answer.

The general outline becomes as follows:

    (defui MyComponent
        Object
        (initLocalState [this] {:id (gensym "") ...}) ; <-- the key
        (render [this]
         (let [id (om/get-state this :id)] ...))
        (componentDidmount [this]
         (let [id (om/get-state this :id)] ...))
        ...


This approach is a little more verbose than in Reagent, but yields 
equivalent results. I use this all the time, so am a little surprised I 
couldn't find any discussion about this elsewhere. I guess I am the only 
one who didn't get it the first time :-)


Op woensdag 8 november 2017 16:20:29 UTC+1 schreef Marc Claesen:
>
> I am fairly new to cljs and very new to Om.Next so this question is 
> probably trivial to answer. 
>
>
> I would like to do something equivalent to Reagent form-3 components, 
> which enable me to define some vars I can reuse in all lifecycle functions 
> of specific instantiations of a component.
>
>
> Basically my question is how do I construct something equivalent to the 
> let in the Reagent code snippet below? I can't seem to figure out how to 
> do this properly using Om's defui macro.
>
>
> A simple example would be to generate ids I can attach to specific 
> elements in my component to streamline access to them in all lifecycle 
> functions of a specific instance of my component via gdom/getElement.
>
>
> (defn my-component
>   [x y z]  
>   (let [id (gensym "my-component-")]  ;; <-- how to do this in Om?
>      (reagent/create-class                 
>        {:component-did-mount (...)
>         :component-will-mount (...)          
>         :reagent-render
>          (fn [x y z] (...))))
>
>
> I assume an equivalent approach is possible in Om.Next, but please let me 
> know if I am somehow committing a cardinal sin using this approach.
>
>
> I have also asked this question on stackoverflow 
> <https://stackoverflow.com/q/47181093/2148672>.
>

-- 
Note that posts from new members are moderated - please be patient with your 
first post.
--- 
You received this message because you are subscribed to the Google Groups 
"ClojureScript" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/clojurescript.

Reply via email to