我正在使用组件的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/
请您分享一个例子,您包括参数? –