Search code examples
clojurescriptre-frame

How to abstract `reg-sub` in reframe


in my code ,there is duplication like this:

(reg-sub
 :hello-john
 (fn [ db [ _ say-hi ]
   (str (get-in db [ say-hi ]) "hello John")
 )

(reg-sub
 :hello-jack
 (fn [ db [ _ say-hi ]
   (str (get-in db [ say-hi ]) "hello Jack")
 )

this pattern is quite tedious and I try to factor out with following code in sub.cljs:

(for [ [x y]  [[:hello-john "hello John"] 
                [:hello-jack "hello Jack"]]  ]
 (reg-sub
   x
   (fn [ db [ _ say-hi ]
     (str (get-in db [ say-hi ]) y ))
)

But it desn't work as expect. Thanks for reading this appreciate any help :)


Solution

  • Why not

    (reg-sub
     :say-hello
     (fn [ db [ _ person say-hi ]
       (str (get-in db [ say-hi ]) "hello " person)
     )