javascript - How to access an object member from event callback function in a class object on Ecmascript 6 (ES6) -


i've got :

class register {      render(){         return (<div onchange={this.afunction}></div>)     };      afunction(event){         this.printsomething();  //uncaught typeerror: undefined not function     }      printsomething(){         console.log('horray');     }  } 

how can call printsomething within afunction? es6 confuses me.

thank you.

you'll notice when using es6 classes react a lot has changed.

also, es6 classes don't autobind way es5 react.createclass would.

as result, need bind this of function


you have 2 options

1. use arrow function

render(){     return <div onchange={event => this.afunction(event)}></div>; } 

2. use binding

render(){     return <div onchange={this.afunction.bind(this)}></div>; } 

i assume you're using react.js this. if are, need update

class register 

to

class register extends react.component 

Comments

Popular posts from this blog

What Are The Best Universities In The World?

Hard vs. Soft Water: What's The Difference?

Why People Get Naked Before Freezing To Death