如何在没有 lambda 函数的情况下通过单个处理程序在 React Native 中处理几个 TextInput?
How to handle a few TextInput by single handler in React Native without lambda function?
我需要在 React Native 应用程序中通过单个事件处理程序处理一些输入,但不能使用 lambda 函数,因为它在我的应用程序中被禁止。使用 lambda 函数,您可以只发送第二个参数,它可能是这样的:
<TextInput
placeholder = 'email'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {(text) => { this.handleTwoInputs(text, 'email'); }}
autoCorrect = {false}
/>
<TextInput
placeholder = 'name'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {(text) => { this.handleTwoInputs(text, 'name'); }}
autoCorrect = {false}
/>
handleTwoInputs = (text, type) => {
if(type === 'name'){
this.inputName = text;
} else {
this.inputEmail = text;
}
}
但是没有lambda函数怎么办呢?
您不需要使用箭头函数:
<TextInput
placeholder = 'email'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {function(text) { this.handleTwoInputs(text, 'email'); }}
autoCorrect = {false}
/>
<TextInput
placeholder = 'name'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {function(text) { this.handleTwoInputs(text, 'name'); }}
autoCorrect = {false}
/>
编辑:
好的,我想我误解了这个问题。使用 onChange
而不是 onChangeText
:
可以实现您想实现的目标
<TextInput
name = 'email_input'
placeholder = 'email'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {handleTwoInputs}
autoCorrect = {false}
/>
<TextInput
name = 'name_input'
placeholder = 'name'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {handleTwoInputs}
autoCorrect = {false}
/>
handleTwoInputs = ({ nativeEvent }) => {
const { eventCount, target, text } = nativeEvent
if (target.name === 'name_input') {
this.inputName = text;
} else {
this.inputEmail = text;
}
}
我需要在 React Native 应用程序中通过单个事件处理程序处理一些输入,但不能使用 lambda 函数,因为它在我的应用程序中被禁止。使用 lambda 函数,您可以只发送第二个参数,它可能是这样的:
<TextInput
placeholder = 'email'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {(text) => { this.handleTwoInputs(text, 'email'); }}
autoCorrect = {false}
/>
<TextInput
placeholder = 'name'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {(text) => { this.handleTwoInputs(text, 'name'); }}
autoCorrect = {false}
/>
handleTwoInputs = (text, type) => {
if(type === 'name'){
this.inputName = text;
} else {
this.inputEmail = text;
}
}
但是没有lambda函数怎么办呢?
您不需要使用箭头函数:
<TextInput
placeholder = 'email'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {function(text) { this.handleTwoInputs(text, 'email'); }}
autoCorrect = {false}
/>
<TextInput
placeholder = 'name'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {function(text) { this.handleTwoInputs(text, 'name'); }}
autoCorrect = {false}
/>
编辑:
好的,我想我误解了这个问题。使用 onChange
而不是 onChangeText
:
<TextInput
name = 'email_input'
placeholder = 'email'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {handleTwoInputs}
autoCorrect = {false}
/>
<TextInput
name = 'name_input'
placeholder = 'name'
onSubmitEditing ={Keyboard.dismiss}
onChangeText = {handleTwoInputs}
autoCorrect = {false}
/>
handleTwoInputs = ({ nativeEvent }) => {
const { eventCount, target, text } = nativeEvent
if (target.name === 'name_input') {
this.inputName = text;
} else {
this.inputEmail = text;
}
}