Skip to content

Instantly share code, notes, and snippets.

@john1jan
Last active March 21, 2017 12:52
Show Gist options
  • Save john1jan/4f88598ff0e7b78504a689320d4c6c09 to your computer and use it in GitHub Desktop.
Save john1jan/4f88598ff0e7b78504a689320d4c6c09 to your computer and use it in GitHub Desktop.
Passing Params Child Component
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = { message: 'I am parent ' };
this.printParent = this.printParent.bind(this);
}
render() {
return <div>
<p>Parent Component</p>
<ChildComponent printParent={this.printParent} />
</div>
}
printParent(valueFromChild) {
console.log("printParent", this);
console.log(this.state.message);
console.log("Value from child:", valueFromChild);
}
}
class ChildComponent extends React.Component {
constructor(props) {
super(props);
this.state = { message: 'I am Child ' };
}
render() {
return <div>
<p> Child Component</p>
<input type="button" value="Print From Child" onClick={this.props.printParent("Mars")} />
</div>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment