- var Child = React.createClass({
- handleClick: function(event){
- if (confirm("Press a button!")) {
- this.props.sendChildConfirm();
- } else {
- this.props.sendChildCancel();
- }
- }
- ,
- render: function () {
- return (
- <a style={{border: 1}} onClick={this.handleClick}>update parent state from within child</a>
- );
- },
- });
- var Parent = React.createClass({
- getInitialState: function() {
- return {childText: 'Nothing'};
- },
- render: function () {
- return (
- <div>
- <h3>Child says {this.state.childText}</h3>
- <Child sendChildConfirm={this.handleConfirm} sendChildCancel={this.handleCancel} />
- </div>
- );
- },
- handleConfirm: function(event) {
- this.setState({childText: 'Yes!'});
- },
- handleCancel: function(event) {
- this.setState({childText: 'No!'});
- }
- });
- ReactDOM.render(
- <Parent />,
- document.getElementById('react-child-parent-dom-node')
- );
Download full working code : Download
No comments:
Post a Comment