从React Native中的AsyncStorage设置TextInput值

时间:2018-08-13 11:15:18

标签: react-native react-native-android react-native-ios textinput asyncstorage

我已成功将我的电子邮件和密码值存储在ReactNative的 AsyncStorage 中。

现在,我还能够在应用程序启动时从 AsyncStorage 获取值。

我现在正在使用Alert.alert()显示值。

我很困惑如何在两个TextInputs中设置该值:

1。电子邮件和2.密码。

我对状态一无所知。使用状态可能是可能的。 但是如何?

关于如何动态更改或设置TextInput中的值的任何想法?

谢谢。

编辑

状态:

state = {
    email: '',
    password: '',
    register: false,
    activity: false,
    isRemember:false,
  }



componentWillMount(){
       //To handle Remember Me :
       this._checkForRememberMe();
  }

登录成功:

this._storeRememberME();

_storeRememberME=async()=>{
     //SAVING ASYNCSTORAGE VALUE FOR REMEMBER ME : 
     Alert.alert("is Remember state >>"+this.state.isRemember);
     if(this.state.isRemember){
          await AsyncStorage.setItem('username', this.state.email);
      await AsyncStorage.setItem('password', this.state.password);  
  Alert.alert("is Remember state inside >>"+this.state.isRemember);
      await AsyncStorage.setItem('isRemember', 'yes');
   }else{
     const isRemember =AsyncStorage.getItem('rememberMe');
     if(isRemember){
      Alert.alert("Async clear "+this.state.isRemember);
       await AsyncStorage.clear();
     }
   }
  }

  // Fetch the token from storage then navigate to our appropriate place
  _checkForRememberMe = async () => {
    const isRemember = await AsyncStorage.getItem('isRemember');
    if(isRemember){

      const username = await AsyncStorage.getItem('username');
      const password = await AsyncStorage.getItem('password');

      this.state.email=username;
      this.state.password=password;
      this.state.checkedRemember=true;
      Alert.alert(""+this.state.email+""+this.state.password);

    }else{
      this.state.checkedRemember=false;
    }
  }

_handleCheck(val){

if(val){
  this.state.isRemember=true;
}else{
  this.state.isRemember=false;
}

Alert.alert("is remember", ">>>"+val);

}

复选框如下:

<Checkbox
                onChange={(val) => this._handleCheck(val)}
                    style={{marginRight:8}}
                    checked={checkedRemember}
                    checkedColor={$primaryBlue}
                    uncheckedColor={$lightGray}
                    iconName='matMix'
                />

1 个答案:

答案 0 :(得分:0)

您可以在您的状态下获取变量并将此变量设置为TextInput。然后在构造函数中将AsyncStorage的值设置为您的状态。因为状态反映到视图。因此,当您从AsyncStorage获得价值时,您将在TextInput中看到价值。

一个很好的例子。

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  TextInput,
  Button,
  View,
  AsyncStorage
} from 'react-native';

export default class AsyncStorageExample extends Component {

  constructor(props) {
    super(props);
    this.state = {
        myKey: null
    }
  }

  async getKey() {
    try {
      const value = await AsyncStorage.getItem('@MySuperStore:key');
      this.setState({myKey: value});
    } catch (error) {
      console.log("Error retrieving data" + error);
    }
  }

  async saveKey(value) {
    try {
      await AsyncStorage.setItem('@MySuperStore:key', value);
    } catch (error) {
      console.log("Error saving data" + error);
    }
  }

  async resetKey() {
    try {
      await AsyncStorage.removeItem('@MySuperStore:key');
      const value = await AsyncStorage.getItem('@MySuperStore:key');
      this.setState({myKey: value});
    } catch (error) {
      console.log("Error resetting data" + error);
    }
  }

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to Demo AsyncStorage!
        </Text>

        <TextInput
          style={styles.formInput}
          placeholder="Enter key you want to save!"
          value={this.state.myKey}
          onChangeText={(value) => this.saveKey(value)}
          />

        <Button
          style={styles.formButton}
          onPress={this.getKey.bind(this)}
          title="Get Key"
          color="#2196f3"
          accessibilityLabel="Get Key"
        />

        <Button
          style={styles.formButton}
          onPress={this.resetKey.bind(this)}
          title="Reset"
          color="#f44336"
          accessibilityLabel="Reset"
        />

        <Text style={styles.instructions}>
          Stored key is = {this.state.myKey}
        </Text>


      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    padding: 30,
    flex: 1,
    alignItems: 'stretch',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  formInput: {
    paddingLeft: 5,
    height: 50,
    borderWidth: 1,
    borderColor: "#555555",
  },
  formButton: {
    borderWidth: 1,
    borderColor: "#555555",
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
    marginTop: 5,
  },
});

AppRegistry.registerComponent('AsyncStorageExample', () => AsyncStorageExample)