ReactNative

React Native 存储之 AsyncStorage

2019-03-25  本文已影响2人  Kevin_小飞象

简介

方法

实例

1. 效果图
(1)在文本输入框中输入姓名、电话后,点击“保存”按钮即可通过 AsyncStorage 存储起来。
(2)退出程序后再次打开,程序又会自动加载之前保存的信息。
(3)点击“清除”按钮则将本地保存的数据全部清除。

img01.jpg
img02.jpg

2. 样例代码
组件

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

export default class UserInfo extends Component {
  constructor(props) {
    super(props);
    this.state = {
      name: '',
      phone: ''
    }
  }

  componentDidMount() { 
    var _that = this;
    var keys = ['name', 'phone'];

    AsyncStorage.multiGet(keys, function (errs, result) {
      if (errs) {
        return;
      }

      _that.setState({
        name: (result[0][1] != null) ? result[0][1] : '',
        phone: (result[1][1] != null) ? result[1][1] : ''
        
      });
    });
  }
  render() { 

    return (
        <View style={styles.flex}>
            <View style={styles.row}>
            <View style={styles.head}>
              <Text style={styles.label}>name</Text>
            </View>
            <View style={styles.flex}>
              <TextInput style={styles.input}
                value={this.state.name}
                onChangeText={(name) => this.setState({name})}/>
            </View>
            </View>
            
            <View style={styles.row}>
            <View style={styles.head}>
              <Text style={styles.label}>phone</Text>
            </View>
            <View style={styles.flex}>
              <TextInput style={styles.input}
                value={this.state.phone}
                onChangeText={(phone) => this.setState({phone})}/>
            </View>
          </View>
        <View style={styles.row}>
              <Text style={styles.btn} onPress={this.save.bind(this)}>save</Text>
              <Text style={styles.btn} onPress={this.clear.bind(this)}>clear</Text>
          </View>
      </View>
    );
  }
    
    //保存数据
    save() {
        //设置多项
        var keyValuePairs = [
            ['name', this.state.name],
            ['phone', this.state.phone]
        ]
        AsyncStorage.multiSet(keyValuePairs, function (errs) {
            if (errs) {
                //TODO:存储出错
                return;
            }
            alert('数据保存成功!');
        });
    }

    //清除数据
  clear() {
    var _that = this;
    AsyncStorage.clear(function(err){
      if(!err){
        _that.setState({
          name: "",
          phone: ""
        });
        alert('存储的数据已清除完毕!');
      }
    });
  }
}

const styles = StyleSheet.create({
    flex: {
        flex: 1,
    },
    topStatus: {
        marginTop: 25,
    },
    row: {
        flexDirection: 'row',
        height: 45,
        marginBottom: 10
    },
    head: {
        width: 70,
        marginLeft: 5,
        backgroundColor: '#23BEFF',
        height: 45,
        justifyContent: 'center',
        alignItems: 'center'
    },
    label: {
        color: '#fff',
        fontSize: 15,
        fontWeight: 'bold'
      },
    input: {
        height: 45,
        borderWidth: 1,
        marginRight: 5,
        paddingLeft: 10,
        borderColor: '#ccc'
      },
      btn: {
        flex: 1,
        backgroundColor: '#FF7200',
        height: 45,
        textAlign: 'center',
        color: '#fff',
        marginLeft: 5,
        marginRight: 5,
        lineHeight: 45,
        fontSize: 15,
      },
  
});

使用

import React, {Component} from 'react';
import {
  StyleSheet,  
  View,
} from 'react-native';

import UserInfo from './src/UserInfo';

export default class App extends Component {
  render() { 
    return (
      <View style={styles.container}>
        <UserInfo/>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop:25,
    backgroundColor: '#F5FCFF',
  },
  
});

参考

相关文章:
AsyncStorage ·React Native 中文网
从零学React Native之13 持久化存储
《ReactNative》之sqlite
React Native调用原生端sqlite数据库

上一篇 下一篇

猜你喜欢

热点阅读