我有一個input
HTML標籤,其中onChange
目前如何在React中綁定函數時傳遞事件參數?
onChange={() => { this.props.someFunc(this.props.someVal, e.target.checked) }
不過,我想跟着ES-皮棉no-bind
規則(我想避免內聯函數),我hadling的有問題這個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,但至今沒有運氣。我需要做什麼才能將這兩個值和事件傳遞給綁定函數?
這是一個內聯函數。正如我在帖子中所說的,我想避免這種情況。 – AlanH
我的不好,不知道這個規則意味着沒有內聯函數。您仍然可以使用onClick處理程序創建一個新組件。檢查這些[es-lint protips]的第二個示例(https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-no-bind.md#protips) – Fleezey
是的,這正是我想要遵循的。但不清楚如何處理事件和附加價值。 – AlanH