Skip to content

Instantly share code, notes, and snippets.

@biglovisa
Last active December 15, 2015 04:54
Show Gist options
  • Save biglovisa/018bd9953c993bdf02bc to your computer and use it in GitHub Desktop.
Save biglovisa/018bd9953c993bdf02bc to your computer and use it in GitHub Desktop.
react: state vs props
//-------------------------------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