Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48af9c7

Browse files
chenglouzpao
authored andcommitted
docs tips parent-child communication
1 parent 1da10d7 commit 48af9c7

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

docs/_data/nav_tips.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@
2626
title: Load Initial Data via AJAX
2727
- id: false-in-jsx
2828
title: False in JSX
29+
- id: communicate-between-components
30+
title: Communicate Between Components

docs/tips/13-false-in-jsx.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ title: False in JSX
44
layout: tips
55
permalink: false-in-jsx.html
66
prev: initial-ajax.html
7+
next: communicate-between-components.html
78
---
89

910
Here's how `false` renders in different contexts:
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
id: communicate-between-components
3+
title: Communicate Between Components
4+
layout: tips
5+
permalink: communicate-between-components.html
6+
prev: false-in-jsx.html
7+
---
8+
9+
For parent-child communication, simply [pass props](/react/docs/multiple-components.html).
10+
11+
For child-parent communication:
12+
Say your `GroceryList` component has a list of items generated through an array. When a list item is clicked, you want to display its name:
13+
14+
```js
15+
/** @jsx React.DOM */
16+
17+
var GroceryList = React.createClass({
18+
handleClick: function(i) {
19+
console.log('You clicked: ' + this.props.items[i]);
20+
},
21+
22+
render: function() {
23+
return (
24+
<div>
25+
{this.props.items.map(function(item, i) {
26+
return (
27+
<div onClick={this.handleClick.bind(this, i)} key={i}>{item}</div>
28+
);
29+
}, this)}
30+
</div>
31+
);
32+
}
33+
});
34+
35+
React.renderComponent(
36+
<GroceryList items={['Apple', 'Banana', 'Cranberry']} />, mountNode
37+
);
38+
```
39+
40+
Notice the use of `bind(this, arg1, arg2, ...)`: we're simply passing more arguments to `handleClick`. This is not a new React concept; it's just JavaScript.

0 commit comments

Comments
 (0)