Last active
December 15, 2015 04:54
-
-
Save biglovisa/018bd9953c993bdf02bc to your computer and use it in GitHub Desktop.
react: state vs props
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
//-------------------------------Main (parent) component | |
var Main = React.createClass({ | |
// props: name (data) | |
// state: counter (data) | |
getInitialState: function() { | |
return { counter: 0 }; | |
}, | |
handleClick: function() { | |
this.setState({ counter: ++this.state.counter }); | |
}, | |
render: function() { | |
return ( | |
<div> | |
<div>Hello {this.props.name}</div> | |
<Secondary onButtonClick={this.handleClick} | |
counter ={this.state.counter}/> | |
</div> | |
); | |
}, | |
}); | |
//------------------------------Secondary (child) component | |
var Secondary = React.createClass({ | |
// props: counter (data), onButtonClick (function reference) | |
// state: | |
render: function() { | |
return ( | |
<div> | |
<h1>counter: {this.props.counter}</h1> | |
<button onClick={this.props.onButtonClick}> | |
Click Me! | |
</button> | |
</div> | |
); | |
}, | |
}); | |
ReactDOM.render( | |
<Main name="World" />, | |
document.getElementById('container') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment