This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-07-09
Channels
- # beginners (20)
- # boot (4)
- # cider (2)
- # cljs-dev (25)
- # clojure (45)
- # clojure-dev (1)
- # clojure-greece (5)
- # clojure-italy (20)
- # clojure-nl (12)
- # clojure-russia (11)
- # clojure-uk (256)
- # clojurescript (176)
- # data-science (33)
- # datomic (47)
- # docs (1)
- # duct (13)
- # fulcro (54)
- # graphql (24)
- # hoplon (3)
- # jobs (1)
- # leiningen (32)
- # luminus (3)
- # midje (1)
- # mount (2)
- # off-topic (3)
- # onyx (5)
- # overtone (1)
- # parinfer (12)
- # pedestal (4)
- # re-frame (60)
- # reagent (11)
- # reitit (3)
- # ring-swagger (21)
- # rum (1)
- # shadow-cljs (16)
- # spacemacs (23)
- # tools-deps (19)
- # vim (79)
I haven’t been able to find much in the way of documentation on this question so I thought it best to ask here:
When creating a component that requires local state, is it advisable to use (reagent/set-state this {:foo 1})
/`(:foo (reagent/state this))` or the !state (reagent/atom ...)
/`(:foo @!state)` pattern? I’m aware that the former uses an atom as well, but I’m struggling to access the component state in reagent-render
when using the former.
@nick828 I think it’s more common to use the latter reagent/atom
way, or at least haven’t worked with any codebases that used reagent/set-state
in any notable fashion
Thanks @martinklepsch, is the reagent/set-state
usage considered to be old/deprecated then?
usually if you have some mutable state, you put those in a ref of some kind (e.g. an atom) to manage them. reagent atom's allow you maintain that idiom
@nick828 I would say it's deprecated
the funny thing is it doesn't actually use React's this.state
- it also uses a Ratom internally
much better to simply use a ratom bound in a let binding: (defn my-ui [] (let [!state (r/atom nil)] (fn [props] [:input {:on-change (fn [e] (reset! !state (-> e .-target .-value)))}])))
makes it obvious what's going on
if you want to use this.state
, that's also possible though not convenient. See this PR by @juhoteperi: https://github.com/reagent-project/reagent/pull/381/files
IME you need that only when you're dealing with input fields