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?
// 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
Post a Comment