如何向 React 中的 onChange 函数传递附加参数

jet*_*ski 3 typescript reactjs react-native

所以,我遇到了这个问题:我这里有这种类型的元素,显然在更改输入时有事件,是否可以像这样传递参数并访问事件?如果是这样,我该怎么办?

// this function accepts two other params index, stringValue
function onChange(index ,stringValue) {
    console.log(event.target.value);
    console.log("index?: " + index + " and value" + stringValue)
}

//this input is in Child Component
const index = 69;
const someRandomValue='iamstring';
<input defaultValue={name} onChange={onChange(index,iamstring)} />
Run Code Online (Sandbox Code Playgroud)

Thu*_*eer 7

您可以使用柯里化函数来实现它。

onChange = (index, stringValue) => (event) => {
...
}

...

<input defaultValue={name} onChange={this.onChange(index,iamstring)} />
Run Code Online (Sandbox Code Playgroud)