@@ -141,8 +141,7 @@ clock =
141
141
, color: "blue"
142
142
}
143
143
-- additional Props.*
144
- ]
145
- [ ]
144
+ ] []
146
145
```
147
146
148
147
#### Components with type class constraints re-mount on every render?
@@ -220,3 +219,63 @@ defined that specifies the type parameter with the type class contraint.
220
219
If the component using the ordered list knows that the items are of type
221
220
` Int ` , the component can define ` orderedListInt ` as shown above, and use
222
221
it to render the ordered list instead of ` orderedList ` .
222
+
223
+
224
+ #### Understanding ` Children `
225
+
226
+
227
+ In React, we see the ` children ` prop type from time to time, especially
228
+ when using ` createElement ` . This is an opaque data type, in which we can
229
+ coerce into an ` Array ` , but we cannot create. Usually, when you see a
230
+ ` ReactClass ` that features a ` children :: Children ` prop type, this
231
+ means that the component itself expects children to be supplied as an
232
+ argument to ` createElement ` , in the form of an ` Array ReactElement ` .
233
+
234
+ However, in some circumstances (like a ` ContextConsumer ` ), the ` children `
235
+ prop type might look different, like ` children :: a -> ReactElement ` .
236
+ In this case, it would be better to use ` createLeafElement ` , to supply
237
+ the children _ directly through the props_ , rather than as a separate
238
+ argument.
239
+
240
+ This also means that any leaf-like components should _ not_ define a
241
+ ` children :: Children ` prop - this prop should be treated as the
242
+ _ expectation_ of a children argument. In the clock example above, a
243
+ more proper specification might look like the following:
244
+
245
+ ``` purescript
246
+ module Clock (clockComponent) where
247
+
248
+ import React (ReactClass, SyntheticEventHandler)
249
+ import React.SyntheticEvent (SyntheticEvent)
250
+
251
+ foreign import clockComponent
252
+ :: ReactClass
253
+ { format :: String
254
+ , className :: String
255
+ , onTick :: SyntheticEventHandler SyntheticEvent
256
+ }
257
+ ```
258
+
259
+ ``` purescript
260
+ module Component where
261
+
262
+ import Prelude
263
+
264
+ import Effect.Uncurried (mkEffectFn1)
265
+
266
+ import React as React
267
+ import React.SyntheticEvent as Event
268
+
269
+ import Clock as Clock
270
+
271
+ clock :: React.ReactElement
272
+ clock =
273
+ React.createLeafElement Clock.clockComponent
274
+ { format: "HH:mm:ss"
275
+ , className: "test-class-name"
276
+ , onTick: mkEffectFn1 $ \event -> do
277
+ Event.preventDefault event
278
+ -- etc.
279
+ pure unit
280
+ }
281
+ ```
0 commit comments