This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-12-20
Channels
- # adventofcode (29)
- # announcements (7)
- # aws (1)
- # babashka (3)
- # beginners (43)
- # biff (20)
- # clj-kondo (44)
- # cljs-dev (20)
- # clojure (74)
- # clojure-europe (24)
- # clojure-finland (2)
- # clojure-nl (13)
- # clojure-norway (3)
- # clojurescript (31)
- # code-reviews (1)
- # community-development (12)
- # cursive (3)
- # datomic (6)
- # emacs (1)
- # fulcro (25)
- # interop (7)
- # introduce-yourself (2)
- # leiningen (30)
- # nbb (3)
- # overtone (1)
- # podcasts-discuss (5)
- # polylith (24)
- # practicalli (1)
- # reclojure (1)
- # reitit (13)
- # rum (7)
- # shadow-cljs (12)
- # sql (23)
- # squint (51)
- # test-check (1)
- # testing (2)
- # tools-deps (2)
@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"]]]))
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?
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
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
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.