javascript - 如何将 function.bind(this) 转换为 react native 中的箭头函数?
问题描述
我想将我的函数转换为箭头函数,但是每当我传递道具时,它都会给我未定义的错误。
<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);
}}
/>
文本更改处理程序:
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,
});
};
但我收到未定义的错误。我想我做错了,我该怎么做?
解决方案
问题是 onChangeText 只接受一个参数更改值 查看 react-native Docs 此外,您不需要传递您的状态,因为您始终可以访问它。以下或其中一些应该可以工作
onChangeText={(text) => {
textChangeHandler(formState.inputValues.title, text);
}}
您不是在调用该函数只是内联创建它。它需要匹配它所使用的道具的签名。
推荐阅读
- javascript - 谷歌翻译元素获取当前语言
- c# - 为什么它计算一些数字正确而一些数字错误?C#
- css - 每行的最后一个元素在悬停时将下一行的第一个元素推向右侧
- python - Python 发布请求使用请求库引发 400 'Bad Request' 错误,但适用于 cURL
- sql - 如何使 OR 函数只显示一个结果
- macos - MacOS + Visual Studio Code:通过路径将文件夹添加到工作区
- flutter - 混淆何时使用 Child 和 children
- html - 在 NextJS 页面之间导航时如何保持当前的 tabIndex
- c# - 无法让 StateHasChanged 更新 @if 语句
- spring-boot - GraphiQL 不发送 cookie?