react

"mulling over state management stuff"
martinklepsch 2020-12-18T15:59:02.046700Z

Is it stupid to just (hash a-bunch-of-things) if I want to trigger the effect to re-run whenever a-bunch-of-things change?

lilactown 2020-12-18T16:51:03.048700Z

IIRC there are times when hash will return a different number for the same value. it would be ideal for something that's best effort

lilactown 2020-12-18T16:53:07.049200Z

what's stopping you from using a-bunch-of-things instead of hashing it?

martinklepsch 2020-12-18T17:01:53.049700Z

I thought React won’t do a deep compare in that case?

lilactown 2020-12-18T17:23:01.050100Z

it does not, that's correct

lilactown 2020-12-18T17:23:09.050400Z

does a-bunch-of-things change every render?

martinklepsch 2020-12-18T17:46:12.051200Z

No, it doesn’t change often. But I’m now realizing that hashing is probably the same as using the thing directly

lilactown 2020-12-18T18:39:39.051600Z

if the reference identity is stable, then I would just use the thing directly

lilactown 2020-12-18T18:40:31.052500Z

e.g.

(def a-bunch-of-things [1 2 3 4])
the vector will only be created once, and React will always see the same object so it will only fire the effect once

lilactown 2020-12-18T18:42:02.054300Z

where you run into issues is if you have something like this inside a render:

(let [a-bunch-of-things [1 2 3 4]]
  (react/useEffect
    #(do-thing)
    #js [a-bunch-of-things]))
now the vector object gets reconstructed every render (even though it's the same value) and so React will see a new object and fire the effect every render