【发布时间】:2017-07-03 05:12:36
【问题描述】:
我有一个input HTML 标签,onChange 目前在哪里
onChange={() => { this.props.someFunc(this.props.someVal, e.target.checked) }
但是,我想遵循 es-lint no-bind 规则(我想避免使用内联函数),但我在处理这个 onChange 函数的参数时遇到了问题。
在我的构造函数中,我有:
constructor() {
super();
this.state = {
// some state
};
this._onChangeHandler = this._onChangeHandler.bind(this);
}
_this.onChangeHandler = (event, val) => {
this.props.someFunc(event.target.checked, val);
}
render() {
<div>
{
this.props.inputs.map((x) => {
const someValue = // ...a calculated value
return (
<label
}>
<input
onChange={ this._onChangeHandler(someValue) } // need BOTH someValue and the event
checked={ aBool }
type="checkbox"
value={ anotherValue }/>
<span>{ textHere }</span>
</label>
);
})
}
</div>
}
我查看了this post,但到目前为止还没有运气。我需要做什么才能将值和事件传递给绑定函数?
【问题讨论】:
标签: javascript reactjs ecmascript-6 bind