Skip to content

Instantly share code, notes, and snippets.

@StevenLangbroek
Created June 9, 2015 21:02
Show Gist options
  • Save StevenLangbroek/4d9f2c93cd561d0841b4 to your computer and use it in GitHub Desktop.
Save StevenLangbroek/4d9f2c93cd561d0841b4 to your computer and use it in GitHub Desktop.
Child -> Parent communication in React
import React, { Component } from 'react';
class Parent extends Component {
getInitialState(){
return {
title: 'Hello world!';
}
}
render(){
return (
<div>
<h2>{this.state.title}</h2>
<Child setParentState={this.setState.bind(this)} />
</div>
)
}
}
class Child extends Component {
propagateToParent(e){
e.preventDefault();
this.props.setParentState({ title: 'Goodbye cruel world!' });
}
render(){
return (
<p>You should be able to <a href="#" onClick={this.propagateToParent.bind(this)}>click me</a></p>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment