Created
June 18, 2016 06:26
-
-
Save markerikson/64e8033dabab41acf31b9f3b1118462a to your computer and use it in GitHub Desktop.
React state vs props comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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