admin管理员组文章数量:1312702
I am trying to make Search Bar. I want user to type something in TextInput
and when user press 'Enter' I want to call my function.
<TextInput
//here when user press enter call function()
style={styles.searchStyle}
allowFontScaling={false}
placeholder="Search"
onChangeText={(val) => {setSearch(val)}}
/>
I tried onSubmitEditing
but it doesn't work.
I am trying to make Search Bar. I want user to type something in TextInput
and when user press 'Enter' I want to call my function.
<TextInput
//here when user press enter call function()
style={styles.searchStyle}
allowFontScaling={false}
placeholder="Search"
onChangeText={(val) => {setSearch(val)}}
/>
I tried onSubmitEditing
but it doesn't work.
2 Answers
Reset to default 8import React, { Component } from 'react' import {TextInput} from 'react-native'
const Trial =()=>{
const onSubmitted=()=>{
console.log('Submitted')
}
return(
<TextInput
//here when user press enter call function()
// style={styles.searchStyle}
allowFontScaling={false}
placeholder="Search"
onSubmitEditing={()=>onSubmitted()}
/>
)
}
export default Trial
onSubmitEditing it works fine, I hope it would be helpful for you
if you are using mutiline={true} in TextInput like <TextInput multiline={true} />
onSubmitEditing will not work, instead it will move to nextLine.
I am doing a workaround, so basically I check if user entered next line, if so, call submit function and dismiss keyboard, you can import {Keyboard} from react-native
<TextInput onChangeText={(txt) => {
if (/\n/.test(txt)) {
//call submit function here
Keyboard.dismiss()
return
} else {
//do something here
}
}} />
本文标签: javascriptHow to call function on 39Enter39 press in TextInputReact NativeStack Overflow
版权声明:本文标题:javascript - How to call function on 'Enter' press in TextInput - React Native - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741882754a2402844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论