@@ -125,8 +125,34 @@ after error, it can be also used to update RAtom as in Reagent the Ratom is avai
125125in function closure even for static methods. `ComponentDidCatch` can be used
126126for side-effects, like logging the error.
127127
128+ ## [Function components](https://reactjs.org/docs/components-and-props.html#function-and-class-components )
129+
130+ JavaScript functions are valid React components, but Reagent implementation
131+ by default turns the ClojureScript functions referred in Hiccup-vectors to
132+ Class components.
133+
134+ However, some React features, like Hooks, only work with Functional components.
135+ There are several ways to use functions as components with Reagent:
136+
137+ Calling `r/create-element` directly with a ClojureScript function doesn't
138+ wrap the component in any Reagent wrappers, and will create functional components.
139+ In this case you need to use `r/as-element` inside the function to convert
140+ Hiccup-style markup to elements, or just returns React Elements yourself.
141+ You also can't use Ratoms here, as Ratom implementation requires the component
142+ is wrapped by Reagent.
143+
144+ Using `adapt-react-class` or `:>` is also calls `create-element`, but that
145+ also does automatic conversion of ClojureScript parameters to JS objects,
146+ which isn't usually desired if the component is ClojureScript function.
147+
148+ New way is to configure Reagent Hiccup-compiler to create functional components:
149+ [Read Compiler documentation](./ReagentCompiler.md )
150+
128151## [Hooks](https://reactjs.org/docs/hooks-intro.html )
129152
153+ NOTE: This section still refers to workaround using Hooks inside
154+ class components, read the previous section to create functional components.
155+
130156Hooks can't be used inside class components, and Reagent implementation creates
131157a class component from every function (i.e. Reagent component).
132158
@@ -184,16 +210,16 @@ If the parent Component awaits classes with some custom methods or properties, y
184210 (r/create-class
185211 {:get-input-node (fn [this] ...)
186212 :reagent-render (fn [] [:input ...])})))
187-
213+
188214[:> SomeComponent
189215 {:editor-component editor}]
190-
216+
191217; ; Often incorrect way
192218(defn editor [parameter]
193219 (r/create-class
194220 {:get-input-node (fn [this] ...)
195221 :reagent-render (fn [] [:input ...])})))
196-
222+
197223[:> SomeComponent
198224 {:editor-component (r/reactify-component editor)}]
199225```
0 commit comments