我需要在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函数怎么办呢?
答案 0 :(得分:1)
您不需要使用箭头功能:
<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;
}
}