Skip to content

Commit 12ad8ec

Browse files
authored
Merge pull request #19 from chantastic/fix-children-pass-through
fix children-pass-through
2 parents 0ffb87f + b2a2d9f commit 12ad8ec

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

README.markdown

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -358,35 +358,28 @@ Many developers favor [Higher Order Components](#higher-order-component) for thi
358358

359359
## Children pass-through
360360

361-
You might create a component designed to apply `context` and render its `children`.
361+
There are times you'll need to wrap a stateless function with lifecycle events.
362+
While we want to wrap component functionality around other components, we don't want to introduce extraneous DOM nodes.
363+
In some apps, this might brake styling.
364+
365+
We use the function `React.Children.only`.
366+
`only` allows us to return `this.props.children` __if__ there is only one child.
367+
Otherwise, it throws an error.
362368

363369
```js
364-
class SomeContextProvider extends React.Component {
365-
getChildContext() {
366-
return {some: "context"}
370+
class SomeLifeCycleWrapper extends React.Component {
371+
componentDidMount() {
372+
console.log("I mounted but have no DOM.")
367373
}
368374

369375
render() {
370-
// how best do we return `children`?
376+
return React.Children.only(this.props.children)
371377
}
372378
}
373379
```
374380

375-
You're faced with a decision. Wrap `children` in an extraneous `<div />` or return `children` directly. The first options gives you extra markup (which can break some stylesheets). The second will result in unhelpful errors.
381+
In cases where you're working with `state` or `context`, prefer [higher-order components](#higher-order-component) or [render callbacks](#render-callback).
376382

377-
```js
378-
// option 1: extra div
379-
return <div>{children}</div>
380-
381-
// option 2: unhelpful errors
382-
return children
383-
```
384-
385-
It's best to treat `children` as an opaque data type. React provides `React.Children` for dealing with `children` appropriately.
386-
387-
```js
388-
return React.Children.only(this.props.children)
389-
```
390383

391384
## Proxy component
392385

0 commit comments

Comments
 (0)