re-frame

Luke Johnson 2024-05-29T17:53:51.572539Z

Hi all. I’m pretty new to re-frame, coming into a new code base. I am seeing a pattern where we are rendering multiple text fields dynamically. For each section, there’s a static list of field definitions. In the elements that render to be selects, there is an inline subscription to where the options live in the db. E.g.

(defn fields
  []
  [{:label      "First Name"
    :input-type :text}
   {:label      "Last Name"
    :input-type :text}
   {:label      "Suffix"
    :input-type :select
    :options    (rf/subscribe [::subs/suffixes])}])

(map
 (fn [field]
   ^{:key (:id field)}
   [->input field])
 (fields))
That seems weird to me but, as I said, I’m not familiar with best practices and patterns for re-frame. Is this alright to do or should I pass in the atom to fields?

p-himik 2024-05-29T18:53:30.520909Z

LGTM with a caveat that fields should be documented/marked in some way as a function that should only be used in a reactive context. Passing a ratom into fields would be more weird because then you spread the logic between fields and the components that use it (they would all have to know which sub to use and pass to fields). As an alternative, you can turn the fields function into a ::fields subscription.

Luke Johnson 2024-05-29T19:16:11.350499Z

> As an alternative, you can turn the fields function into a ::fields subscription. I hadn’t considered that! That’s really interesting! Thanks for your insight!