mirror of
https://github.com/zebrajr/react.git
synced 2026-01-15 12:15:22 +00:00
Merge pull request #602 from chenglou/tips-bind
docs tips pass arguments to callbacks
This commit is contained in:
@@ -26,3 +26,5 @@
|
||||
title: Load Initial Data via AJAX
|
||||
- id: false-in-jsx
|
||||
title: False in JSX
|
||||
- id: communicate-between-components
|
||||
title: Communicate Between Components
|
||||
|
||||
@@ -4,6 +4,7 @@ title: False in JSX
|
||||
layout: tips
|
||||
permalink: false-in-jsx.html
|
||||
prev: initial-ajax.html
|
||||
next: communicate-between-components.html
|
||||
---
|
||||
|
||||
Here's how `false` renders in different contexts:
|
||||
|
||||
40
docs/tips/14-communicate-between-components.md
Normal file
40
docs/tips/14-communicate-between-components.md
Normal file
@@ -0,0 +1,40 @@
|
||||
---
|
||||
id: communicate-between-components
|
||||
title: Communicate Between Components
|
||||
layout: tips
|
||||
permalink: communicate-between-components.html
|
||||
prev: false-in-jsx.html
|
||||
---
|
||||
|
||||
For parent-child communication, simply [pass props](/react/docs/multiple-components.html).
|
||||
|
||||
For child-parent communication:
|
||||
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:
|
||||
|
||||
```js
|
||||
/** @jsx React.DOM */
|
||||
|
||||
var GroceryList = React.createClass({
|
||||
handleClick: function(i) {
|
||||
console.log('You clicked: ' + this.props.items[i]);
|
||||
},
|
||||
|
||||
render: function() {
|
||||
return (
|
||||
<div>
|
||||
{this.props.items.map(function(item, i) {
|
||||
return (
|
||||
<div onClick={this.handleClick.bind(this, i)} key={i}>{item}</div>
|
||||
);
|
||||
}, this)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
React.renderComponent(
|
||||
<GroceryList items={['Apple', 'Banana', 'Cranberry']} />, mountNode
|
||||
);
|
||||
```
|
||||
|
||||
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.
|
||||
Reference in New Issue
Block a user