Fork me on GitHub
#reagent
<
2022-02-17
>
Richard Bowen17:02:28

Hey. What's the equivalent to `useEffect` in Reagent? Is it just a function that does some work and mutates an atom?

p-himik17:02:45

A rough equivalent is reaction.

lilactown17:02:56

technically using component-did-update in a form 3 component is the closest equivalent of useEffect in reagent

Ben Ritchie18:02:48

I actually googled this a couple of days ago, is it not advisable to use the useEffect hook directly?

lilactown19:02:19

functions used as reagent components get turned into react classes under the hood, which cannot use hooks within the body of the render function

lilactown19:02:11

reagent does have new capability to render a function as a true function component (rather than a class) using the :f> kw. e.g.

(defn my-component
  []
  (useEffect #(prn "rendered"))
  [:div "hi"])

(defn app
  []
  [:f> my-component])

Richard Bowen19:02:05

Yea, appending :f to my-component is required, defn my-component:f [] ...)

Ben Ritchie20:02:22

Right, that's what I was suggesting / asking about

Ben Ritchie20:02:24

Functional components+hooks are generally easier to manage in Typescript, nice lifecycle management syntax etc. Not sure about Reagent, still learning