reactjs Onclick event ,sending value to parent

reactjs Onclick event ,sending value to parent

by Rich Costello

HTML

<script src="https://unpkg.com/[email protected]/umd/react.development.js"></script>
<script src="https://unpkg.com/[email protected]/umd/react-dom.development.js"></script>

<div id="container">
    <!-- This element's contents will be replaced with your component. -->
</div>

CSS

.widget {
    width: 202px;
    margin:100px auto;
//    border:1px solid black;
    padding:20px;
}
.name {
    color: red;
}
.default-label {
    border: 1px solid black;
    padding: 10px;
    margin-bottom: 10px;
    width:180px;
}

.green-label {
    border: 1px solid green;
    padding: 10px;
    margin-bottom: 10px;
    width: 180px;
}

.default-input {
    border: 1px solid #eaeaea;
    padding: 10px;
    width:180px;
}

.important-input {
    border: 2px solid red;
    width:180px;
}

img.center {
    width: 100px;
    height: 100px;
    display: block;
    margin: 15px auto;    
}

Babel + JSX

class HelloWidget extends React.Component{
        constructor(props) {
          super(props);
          this.state = {
                index: 10             
          };
        }

        onModal(index) {
            console.log('in Parent', index);
        }

        render() {
            return (
               <div>
                   <Child onModal={this.onModal.bind(this)} index={this.state.index}/>
              </div>
            );
        }
      }
      
      class Child extends React.Component{
      
      		onModal(){
             this.props.onModal(this.props.index);
          }
          
      		 render() {
          		  return (
                   <div onClick={this.onModal.bind(this)}>Click Me in Child </div>
                );
           }
       }
       
      
ReactDOM.render(<HelloWidget/>, document.getElementById('container'));