helix

https://github.com/Lokeh/helix
2020-10-06T19:50:29.005500Z

@wilkerlucio I've got to a point that I need a pointer with use-state, and I saw you use-atom-state example on zulip log. Do you have anything else to add to that implementation ? Have you gone for it ? https://gist.github.com/geraldodev/a27a7ec31c91610190b9e3911a2f54c6

wilkerlucio 2020-10-06T19:52:38.006800Z

@geraldodev hello, I did used something similar in the past, but it's been a while, I personally like the approach, just not sure if that implementation as-is is the best, one thing you can improve on that is use a defrecord to define a type instead of generating one every time with reify

wilkerlucio 2020-10-06T19:53:54.007100Z

I found something here:

wilkerlucio 2020-10-06T19:53:55.007400Z

(deftype ReactAtomState [value set-value!]
  IDeref
  (-deref [o] value)

  IReset
  (-reset! [o new-value] (doto new-value set-value!))

  ISwap
  (-swap! [a f] (set-value! (f value)))
  (-swap! [a f x] (set-value! (f value x)))
  (-swap! [a f x y] (set-value! (f value x y)))
  (-swap! [a f x y more] (set-value! (apply f value x y more))))

(defn use-state-atom [initial-value]
  (let [[value set-value!] (use-state initial-value)]
    (->ReactAtomState value set-value!)))

2020-10-06T19:54:44.008Z

@wilkerlucio thank you.

lilactown 2020-10-06T19:55:17.008800Z

I will caution that this couples reading with writing, and also doesn’t act exactly the same as an atom

lilactown 2020-10-06T19:56:08.010400Z

swapping and then derefing in the same tick will still yield the old value until next render

lilactown 2020-10-06T19:57:12.012600Z

As long as you’re fine with that, go forth. But those were the reasons I ended up removing the hook from helix core hooks

lilactown 2020-10-06T19:58:39.013900Z

It can certainly be useful sometimes. I find that passing in a value and an on-change callback is what I prefer nowadays

2020-10-06T20:03:22.014600Z

(defnc ContratualizacaoGraph [] (let [ [pilha-vis set-pilha-vis] (hooks/use-state []) fn-conj-nivel (fn [{:keys [type] :as m}] )

] )

2020-10-06T20:06:34.017400Z

@lilactown I need pass function fn-conj-nivel on the render to allow changing of the state. The problem is that pilha-vis is a value and not a pointer, and set-pilha-vis changes the entire value. I need fn-conj-nivel to refer to the actual state, but because its a value it establish a closure with the initial value ([]).

2020-10-06T20:06:46.017800Z

sorry my english

lilactown 2020-10-06T20:10:34.018300Z

I don’t think that wrapping it in a type that implements deref will help you here

lilactown 2020-10-06T20:11:41.019300Z

fn-conj-nivel as it’s written now will be recreated each render. which means it will close over each new value of pilha-vis

2020-10-06T20:27:25.020300Z

@lilactown You are right

ordnungswidrig 2020-10-06T21:42:53.021900Z

Does anybody know an article or library about using a more event driven approach (similar to re-frame) with hooks? I have the impression that I’m sprinkling use-state or custom hooks which build on use-state all over the code which makes reasoning about the “global” state of the application quite tricky.

lilactown 2020-10-06T22:42:21.022500Z

use-reducer is a nice improvement over use-state for complex components