Skip to content

Commit

Permalink
update demo05 for React.Children
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanyf committed Nov 9, 2015
1 parent cb7cb65 commit 6a6d690
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,14 @@ Components can have attributes, and you can use `this.props.[attribute]` to acce

React uses `this.props.children` to access a component's children nodes.

```js
```javascript
var NotesList = React.createClass({
render: function() {
return (
<ol>
{
this.props.children.map(function (child) {
return <li>{child}</li>
React.Children.map(this.props.children, function (child) {
return <li>{child}</li>;
})
}
</ol>
Expand All @@ -151,7 +151,9 @@ ReactDOM.render(
);
```

Please be minded that only if the component has more than one child node, you could take `this.props.children` as an array, otherwise `this.props.children.map` throws a TypeError.
Please be minded that the value of `this.props.children` has three possibilities. If the component has no children node, the value is `undefined`; If single children node, an object; If multiple children nodes, an array. You should be careful to handle it.

React gave us an utility [`React.Children`](https://facebook.github.io/react/docs/top-level-api.html#react.children) for dealing with the `this.props.children`'s opaque data structure. You could use `React.Children.map` to iterate `this.props.children` without worring its data type being `undefined` or `object`. Check [official document](https://facebook.github.io/react/docs/top-level-api.html#react.children) for more methods `React.Children` offers.

## Demo06: PropTypes ([source](https://github.com/ruanyf/react-demos/blob/master/demo06/index.html))

Expand Down
4 changes: 2 additions & 2 deletions demo05/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
return (
<ol>
{
this.props.children.map(function (child) {
return <li>{child}</li>
React.Children.map(this.props.children, function (child) {
return <li>{child}</li>;
})
}
</ol>
Expand Down

0 comments on commit 6a6d690

Please sign in to comment.