Fork me on GitHub
#rum
<
2022-12-20
>
Dallas Surewood03:12:33

@tonsky Is there a better way to do a default for rum/local than will-mount? This is what I had to write to make a TTRPG character editor component with optional default values

(rum/defcs make-character-page
  < (rum/local
     #:kit.guestbook.spec.player-character
      {:name ""
       :stats
       #:kit.guestbook.spec.player-character {:str nil
                                              :dex nil
                                              :cha nil
                                              :hp nil}}
     ::character-sheet)
  < {:will-mount (fn [state]
                   (if-let [default-stat-values (nth (:run/args state) 3)]
                     (swap! (::character-sheet state) assoc :kit.guestbook.spec.player-character/stats
                            #:kit.guestbook.spec.player-character {:str (:kit.guestbook.spec.player-character/str default-stat-values)
                                                                   :dex (:kit.guestbook.spec.player-character/dex default-stat-values)
                                                                   :cha (:kit.guestbook.spec.player-character/cha default-stat-values)
                                                                   :hp (:kit.guestbook.spec.player-character/hp default-stat-values)})
                     state)
                   state)}
  [state on-back on-submit]
  (let [*character-sheet-state (::character-sheet state)]
    [:div.stack
     [:div "Make a character"]
     (character-sheet *character-sheet-state)
     [:div
      {:style {:display "flex"}}
      [:button.btn {:on-click on-back} "Back"]
      [:button.btn {:on-click (fn [e]
                                (.preventDefault e)
                                (on-submit @*character-sheet-state))} "Create"]]]))

Niki12:12:44

I’m not sure what are you trying to do. Rum/local let you specify default value, and you are using it. What do you mean by default?

Dallas Surewood12:12:54

In react, useState can be passed a default value directly from the components props From what I understand, rum/local let's you specify a default value, but because it's a mixin, I can't just put the symbol from the args in it (because it's written before the args list). So I am using will-mount, which can read the args, to set the local state afterwards. If there's a cleaner way to do this, and I'm just missing it, that'd be great to know

Dallas Surewood08:01:00

Here would be a react example. The form component is passed a name for the form and it will set that as a default with useState

Niki11:01:28

I think you got it right. You can write your own mixin that does what you need, though

Dallas Surewood18:01:56

I'm figuring out how to write a good mixin for this. One of the problems with Mixins is they have no knowledge of the symbols in the function definition. So I could write a mixin that takes an "argument index" so we know which argument is the default for the local state I'm trying to define. But if the arg list changes, I have to remember to change that number. In reagent, you just make a let for an atom and you can give it a default. But of course reagent has to use ratom and rum allows all kinds of state.