コンポーネントのES6クラスを使用してReactjsでアプリケーションを作成しています。 引数を持つクラスの中で関数を呼び出すまで、コードは意図したとおりに動作します。ES6クラスの引数を持つ関数を呼び出す方法は?
SampleClass.js
class SampleClass extends React.Component {
constructor(props, context) {
super(props, context);
this.state = {
backgroundColor: 'yellow'
}
}
onChangeBackgroundColor(backgroundColor) {
this.setState({
backgroundColor: backgroundColor
})
}
render() {
return <div style={{backgroundColor: this.state.backgroundColor, padding: 10}}>
<span onClick={this.onChangeBackgroundColor.bind(this)} style={{background: 'white'}}>
Change element style
</span>
</div>
}
}
React.render(<SampleClass />, document.getElementById('container'));
私はthis.onChangeBackgroundColor.bind(this)
のような引数なしの関数の罰金を呼び出すことができています。
しかし、関数に引数を渡すと、コンソールでエラーが発生します。 Uncaught TypeError: Cannot read property 'bind' of undefined
参考フィドル:https://jsfiddle.net/purezen/s6ap3m8s/3/
をあなたは、引数を含めている例を共有していただけますか? –