如何通过onSubmitEditing将<textinput>的输入值发送到异步函数?

时间:2018-06-20 14:21:14

标签: react-native

我想在react native中保存字段输入的值。如何通过使用onSubmitediting事件将值发送到异步函数?

我尝试过:

<TextInput onSubmitEditing={ (text) => this.Store(text)}></TextInput>

这是我正在使用的功能

Store = async() =>
{
    var textvalue = "here the value of the <TextInput>";
    try 
    {
        await AsyncStorage.setItem('@Temp:city', textvalue );
    } 
    catch (error) 
    {
        console.log(error)
    }
}

1 个答案:

答案 0 :(得分:1)

请尝试以下操作:

   <TextInput 
     onSubmitEditing={ event => {
      const text = event.nativeEvent.text;
      this.Store(text);
    }} >
   </TextInput>

并保存如下:

Store = async(text) => {
     try 
     {
     await AsyncStorage.setItem('@Temp:city', text );

     } 
     catch (error) 
     {
      console.log(error)
     }
 }