Communicate Between Components

For parent-child communication, simply pass props.

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:

  1. var handleClick = function(i, props) {
  2. console.log('You clicked: ' + props.items[i]);
  3. }
  4. function GroceryList(props) {
  5. return (
  6. <div>
  7. {props.items.map(function(item, i) {
  8. return (
  9. <div onClick={handleClick.bind(this, i, props)} key={i}>{item}</div>
  10. );
  11. })}
  12. </div>
  13. );
  14. }
  15. ReactDOM.render(
  16. <GroceryList items={['Apple', 'Banana', 'Cranberry']} />, mountNode
  17. );

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.

For communication between two components that don’t have a parent-child relationship, you can set up your own global event system. Subscribe to events in componentDidMount(), unsubscribe in componentWillUnmount(), and call setState() when you receive an event. Flux pattern is one of the possible ways to arrange this.