Fork me on GitHub
#reagent
<
2018-04-19
>
erwinrooijakkers17:04:46

I was wondering, in plain React you can have the problem that setState is called in a promise, and that the component is already unmounted while the promise is still running. Then you get the error: “Trying to call setState on an unmounted component.” Why have I never seen this error in Reagent? 🙂

justinlee17:04:05

@erwinrooijakkers it’s certainly possible to make that mistake in reagent, but I think with idiomatic reagent you would never use react’s state mechanism: instead you’d just to reagent’s atom mechanism. if you update an atom after an unmount, it won’t cause an error: the atom will get updated but there just won’t be a watcher on it

erwinrooijakkers17:04:23

And in React we have to use this state management mechanism? It doesn’t have something like Reagent atoms?

justinlee17:04:12

you don’t have to use react state. many people use an external library like redux or react saga to implement the so called “flux” one-way dataflow. or something like mobx. mobx is probably most similar to the reagent ratom technique. they both basically implement an observer pattern

justinlee17:04:45

but nothing is as solid as reagent’s atoms because it isn’t possible to fully proxy javascript arrays in the current language, so you have to be very careful to code to a convention with mobx

erwinrooijakkers17:04:57

Thanks clear. Yes redux is used, but setState as well

justinlee17:04:28

right. i guess the most straightforward answer is that if you need setState in react, the idomatic way of doing that in reagent is with a form-2 component and a locally declared ratom

erwinrooijakkers17:04:07

Yeah which is much less to worry about

justinlee17:04:22

somewhere i read (probably on twitter) that the setState interface was a design concession and meant as something transitional. the react people really wanted a declarative immutable style because the problem they were trying to solve was actually code correctness (not speed)

Umar Daraz18:04:29

Hi Everybody Is it possible to use render props pattern with reagent?

justinlee18:04:03

it works exactly like in javascript

Umar Daraz18:04:39

Thanks I m able to make it work with render prop. Some how it was not working with children prop May be I m missing something

justinlee18:04:38

well, keep in mind that reagent has a funny way of distinguishing between children and props. if the first argument to the component function is a map, then that is treated as the “props”. all other arguments are just children.

justinlee18:04:48

but you can pass functions wherever

Umar Daraz19:04:37

I did not know that before Will keep this in mind Thanks again for the help 🙂

lilactown22:04:45

If i use a ratom inside of a function (instead of directly inside of a component), will it still reactively re-render?

lilactown22:04:30

e.g.

(def store (r/atom {:copy {"123" "some copy"))

(defn get-copy [id]
   (get-in [:copy id] @store))

(defn component [id]
  (let [copy (get-copy id)]
    [:div copy]))

gadfly36123:04:32

@lilactown it should rerender if you are calling the function from inside the component (like you are). However, I think there is a subtle bug in the get-copy function.. @store and [:copy id] should be switched.

lilactown23:04:21

hah! 😅 thanks!!