reagent 2024-09-17

is there a functional difference between parent1 and parent2 in this example? as in, is there a functional difference between calling a function which returns hiccup using () vs using []?

(defn my-component []
  [:div "hello"])

(defn parent []
  [:div [my-component] "world"])

(defn parent []
  [:div (my-component) "world"])

obviously () is wrong and possibly illegal in most countries, but does it change the behavior at all in any cases?

it's not wrong, both syntaxes can be used. () will be fully transparent for reagent (can't know if a function was called and doesn't care), [] will create a component with all its features (props, rerender, etc.)

➕ 1

This question is so popular that it even got its own doc instead of being just another entry in the FAQ: https://github.com/reagent-project/reagent/blob/master/doc/UsingSquareBracketsInsteadOfParens.md

👍 1
👉 1
🙏 1

thank you both! remembered reading this years back and it didn't turn up in my very brief googling

sorry about that!

I think a good real-world use of () (not really given in the doc) is small helper functions:

(defn label [name]
  [:label name])
so you can factorize how you transform a very specific feature into html in a function and use it in your components with (label "Firstname")