Skip to content

Instantly share code, notes, and snippets.

@markerikson
Created June 18, 2016 06:26
Show Gist options
  • Save markerikson/64e8033dabab41acf31b9f3b1118462a to your computer and use it in GitHub Desktop.
Save markerikson/64e8033dabab41acf31b9f3b1118462a to your computer and use it in GitHub Desktop.
React state vs props comparison
class ChildComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
counter : 0
};
this.onButtonClick = this.onButtonClick.bind(this);
}
onButtonClick() {
const newCounter = this.state.counter + 1;
this.setState({counter : newCounter});
}
render() {
return (
<div>Parent prop: {this.props.text}</div>
<div>Counter state: {this.state.counter}</div>
<button onClick={this.onButtonClick}>Increment Counter</button>
)
}
}
class ParentComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
text : "Hi there!"
};
}
render() {
return (
<ChildComponent text={this.state.text} />
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment