Skip to content

Commit 65dc041

Browse files
athanclarkethul
authored andcommitted
Better Documentation, subtle typos and syntactic details (#165)
* semicolons * exposing `fragment`, fixing doc typos, adding docs in regards to #164 * better README * typo
1 parent e059deb commit 65dc041

File tree

3 files changed

+70
-8
lines changed

3 files changed

+70
-8
lines changed

Diff for: README.md

+61-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ clock =
141141
, color: "blue"
142142
}
143143
-- additional Props.*
144-
]
145-
[ ]
144+
] []
146145
```
147146

148147
#### 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.
220219
If the component using the ordered list knows that the items are of type
221220
`Int`, the component can define `orderedListInt` as shown above, and use
222221
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+
```

Diff for: src/React.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function createClassWithDerivedState(classCtr) {
6565
return function(getDerivedStateFromProps) {
6666
return function(ctrFn) {
6767
var Constructor = componentImpl(displayName)(ctrFn);
68-
Constructor.getDerivedStateFromProps = function(a, b) { return getDerivedStateFromProps(a)(b) };
68+
Constructor.getDerivedStateFromProps = function(a, b) { return getDerivedStateFromProps(a)(b); };
6969
return Constructor;
7070
};
7171
};
@@ -77,7 +77,7 @@ exports.componentImpl = componentImpl;
7777
exports.componentWithDerivedStateImpl = createClassWithDerivedState(componentImpl);
7878

7979
var pureComponentImpl = createClass(React.PureComponent);
80-
exports.pureComponentImpl = pureComponentImpl
80+
exports.pureComponentImpl = pureComponentImpl;
8181
exports.pureComponentWithDerivedStateImpl = createClassWithDerivedState(pureComponentImpl);
8282

8383
exports.statelessComponent = function(x) { return x; };
@@ -91,7 +91,7 @@ function getProps(this_) {
9191
}
9292
exports.getProps = getProps;
9393

94-
exports.childrenToArray = React.Children.toArray
94+
exports.childrenToArray = React.Children.toArray;
9595

9696
exports.childrenCount = React.Children.count;
9797

Diff for: src/React.purs

+6-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ module React
5858
, childrenCount
5959
, class IsReactElement
6060
, toElement
61+
, fragment
6162
, fragmentWithKey
6263
, Context
6364
, ContextProvider
@@ -186,7 +187,7 @@ instance reactPureComponentSpec ::
186187
) =>
187188
ReactPureComponentSpec props state snapshot given spec
188189

189-
-- | Creates a `ReactClass`` inherited from `React.Component`.
190+
-- | Creates a `ReactClass` inherited from `React.Component`.
190191
component :: forall props state snapshot given spec.
191192
ReactComponentSpec (Record props) (Record state) snapshot given spec =>
192193
String ->
@@ -203,7 +204,7 @@ componentWithDerivedState :: forall props state snapshot given spec.
203204
ReactClass (Record props)
204205
componentWithDerivedState = componentWithDerivedStateImpl
205206

206-
-- | Creates a `ReactClass`` inherited from `React.PureComponent`.
207+
-- | Creates a `ReactClass` inherited from `React.PureComponent`.
207208
pureComponent :: forall props state snapshot given spec.
208209
ReactPureComponentSpec (Record props) (Record state) snapshot given spec =>
209210
String ->
@@ -391,7 +392,9 @@ foreign import createElementImpl :: forall required given children.
391392
foreign import createElementDynamicImpl :: forall required given children.
392393
ReactClass required -> given -> Array children -> ReactElement
393394

394-
-- | Create an element from a React class that does not require children.
395+
-- | Create an element from a React class that does not require children. Additionally it can be used
396+
-- | when the children are represented /only/ through the `children` prop - for instance, a `ContextConsumer`
397+
-- | would be turned into a `ReactElement` with `createLeafElement someContext.consumer { children: \x -> ... }`.
395398
createLeafElement :: forall required given.
396399
ReactPropFields required given =>
397400
ReactClass { | required } ->

0 commit comments

Comments
 (0)