reactjs - React — nested onClick handlers? -


i have button component inside parent component , need button component handle button style onclick of button element, while parent handles click of btn component.

i've got clickhandler working in btn component, not parent. what's best way this?

fiddle

// button component class btnfav extends react.component {   constructor(props) {     super(props);     this.state = {favorited: false};     this.handleclick = this.handleclick.bind(this);   }   handleclick() {     this.setstate({favorited: !this.state.favorited});   }   render() {     var btnstyle = this.state.favorited ? 'btn-fav' : 'btn-notfav';     var btntext = this.state.favorited ? 'favorited' : 'not favorited';     return (       <button classname={btnstyle} onclick={this.handleclick}>{btntext}</button>     );   } }; // parent class btnparent extends react.component {  constructor(props) {     super(props);     this.alsodothis = this.alsodothis.bind(this);   }   alsodothis() {     alert('also this.');   }   render() {     return (       <btnfav onclick={this.alsodothis} />     );   } };  reactdom.render(   <btnparent />,   document.getelementbyid('container') ); 


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -