How do I get the value in TextInput when onBlur is called?

后端 未结 4 2044
生来不讨喜
生来不讨喜 2020-12-29 05:48

In React native, I want to pass the value of the TextInput in the onBlur event handler.

onBlur={(e) => this.validateText(e.target.value)}
<
相关标签:
4条回答
  • 2020-12-29 06:06

    You should use the 'onEndEditing' method instead of the 'onBlur'

    onEndEditing?: function Callback that is called when text input ends.

    onBlur is a component function where onEndEditing is specific for TextInput

    onEndEditing

    This approach works for both multiline and single line.

    <TextInput 
        onEndEditing={(e: any) => 
        {
            this.setState({textValue: e.nativeEvent.text})
        }
    }/>
    
    0 讨论(0)
  • 2020-12-29 06:09

    Simple solution: This way onBlur your state's email will always have the last value changed by the user.

               validate = () => {
                const { email } = this.state
                console.log('Validating Email Here!', email)
               }
    
              <TextInput
               style={styles.input}
               placeholder='E-mail'
               onChangeText={email => this.setState({email})}
               onBlur={e => this.validate()}
             />
    
    0 讨论(0)
  • 2020-12-29 06:11

    <TextInput onBlur={() => alert("This is cute")} placeholder="Enter your budget" />

    0 讨论(0)
  • 2020-12-29 06:18

    In React Native, you can get the value of the TextInput from e.nativeEvent.text.

    Unfortunately, this doesn't work for multiline={true}. One hack around this is to maintain a ref to your TextInput and access the text value through the _lastNativeText property of the component. For example (assuming you've assigned your TextInput component a ref of "textInput"):

    onBlur={() => console.log(this.refs.textInput._lastNativeText)}

    0 讨论(0)
提交回复
热议问题