最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native TextInput onSubmitEditing triggering on every Key Press - Stack Overflow

programmeradmin0浏览0评论

In Code below, the onSubmitEditing event triggers on every key press. Strangely, if I ment out the onChangeText event, onSubmitEditing somestimes seems to trigger, as appropriate, only on return.

Is someone able to explain how I would either, (a) get onSubmitEditing to trigger appropriately even when onChangeText is called, or (b) retrieve the value of the text input in onSubmitEditing event.

render() {
    return (
      <View style={styles.container}>
        <TextInput
          onChangeText={text => this.setState({ text })}
          onSubmitEditing={
            console.log(`onSubmitEditing: ${this.state.text}`)
          }
          placeholder="Enter Text..."
          returnKeyType="done"
          returnKeyLabel="done"
        />
      </View>
    );
  }

I am using

"expo": "^32.0.0",
"react": "16.5.0",
"react-native": "0.57.1",

Thanks!

UPDATE: This seems to work

      onSubmitEditing={a => console.log(`onSubmitEditing: ${this.state.text}`) }

Can someone help me understand what is going on?

In Code below, the onSubmitEditing event triggers on every key press. Strangely, if I ment out the onChangeText event, onSubmitEditing somestimes seems to trigger, as appropriate, only on return.

Is someone able to explain how I would either, (a) get onSubmitEditing to trigger appropriately even when onChangeText is called, or (b) retrieve the value of the text input in onSubmitEditing event.

render() {
    return (
      <View style={styles.container}>
        <TextInput
          onChangeText={text => this.setState({ text })}
          onSubmitEditing={
            console.log(`onSubmitEditing: ${this.state.text}`)
          }
          placeholder="Enter Text..."
          returnKeyType="done"
          returnKeyLabel="done"
        />
      </View>
    );
  }

I am using

"expo": "^32.0.0",
"react": "16.5.0",
"react-native": "0.57.1",

Thanks!

UPDATE: This seems to work

      onSubmitEditing={a => console.log(`onSubmitEditing: ${this.state.text}`) }

Can someone help me understand what is going on?

Share Improve this question edited Jan 23, 2019 at 16:03 marc-dworkin asked Jan 23, 2019 at 15:46 marc-dworkinmarc-dworkin 3364 silver badges16 bronze badges 1
  • 2 onSubmitEditing takes a function as a argument. Before you were passing an expression. – johnborges Commented Jan 23, 2019 at 16:07
Add a ment  | 

1 Answer 1

Reset to default 8

simply because onSubmitEditing is a prop that expecting a function call. When you're doing this onSubmitEditing={console.log('a')}, you're missing the binding and so the reason it's working after you implemented in arrow function

发布评论

评论列表(0)

  1. 暂无评论