【发布时间】:2021-06-16 16:19:30
【问题描述】:
我想将我的函数转换为箭头函数,但每当我传递道具时,它都会给我未定义的错误。
<TextInput
style={styles.input}
value={formState.inputValues.title}
onChangeText={textChangeHandler.bind(this, "title")}
/>
在onChangeText 中,我想写一个箭头函数并将formState.inputValues.title 和text 作为道具传递给textChangeHandler,就像这样,
<TextInput
style={styles.input}
value={formState.inputValues.title}
onChangeText={(formState.inputValues.title, text) => {
textChangeHandler(formState.inputValues.title, text);
}}
/>
textChangeHandler:
const textChangeHandler = (inputIdentifier, text) => {
let isValid = false;
if (text.trim().length > 0) {
isValid = true;
}
dispatchFormState({
type: FORM_INPUT_UPDATE,
value: text,
isValid: isValid,
input: inputIdentifier,
});
};
但我收到未定义的错误。我觉得我做错了,我该怎么做?
【问题讨论】:
-
什么是未定义的?另外,对于你来说,两种情况下 textChangeHandler 的定义是否相同(箭头和非箭头)?
-
@TusharShahi 如果我按照上面的说明进行操作,则会出现语法错误,如果我只传递文本,则
textInput字段无法正常工作。另外,如果在onChangeText中没有传递任何内容,则显示错误Can't find variable: text。 -
首先你的函数需要两个参数。当您使用绑定时,它只会得到一个。
-
@TusharShahi 函数与 bind 工作正常。但我想将其转换为箭头函数。所以这就是我要问的我该怎么做?
-
在你的箭头函数中试试这个,让我们知道会发生什么 textChangeHandler(text);
标签: javascript reactjs react-native this arrow-functions