我想在手机中存储2个文本输入的值,然后使用数据。
我正在尝试保存数据但是只存储了一个字符...有人可以向我解释为什么请这样做?
import React, { Component } from 'react';
import {
TouchableHighlight,
TouchableOpacity,
AsyncStorage,
AppRegistry,
StyleSheet,
Navigator,
TextInput,
ListView,
onPress,
Button,
Image,
Text,
View,
} from 'react-native';
var styles = require('../style.js');
class config extends React.Component {
constructor(props) {
super(props);
this.state = { local: 'test',
ext: 'test_ext',
};
}
componentWillMount() {
AsyncStorage.getItem("myKey").then((value) => {
this.setState({"myKey": value});
}).done();
}
getInitialState() {
return { };}
saveData(value) {
AsyncStorage.setItem("myKey", value);
this.setState({"myKey": value});
}
render(){
// console.log({IP_LOCAL});
return(
<View>
<View style={styles.container}>
<Image source={require('../../img/mob.png')} style={styles.mob_logo}/>
</View>
<View style={{marginTop:100}}>
<Text style={{fontSize:20}}>Hello From config component</Text>
<Text>id: {this.props.id}</Text>
<Text>name: {this.props.name}</Text>
<Text>name: {this.props.myVar}</Text>
<Text style={styles.saved}>
{this.state.myKey}
</Text>
<Text> </Text>
<TextInput
style={{height: 40, borderColor: 'gray', borderWidth: 1}}
onChangeText={(local) => this.saveData(local)}
value=""
/>
</View>
</View>
)
}
}
module.exports = config;
答案 0 :(得分:0)
我用
修复 componentWillMount() {
AsyncStorage.getItem("local").then((value) => {
this.setState({"local": value});
}).done();
AsyncStorage.getItem("ext").then((value) => {
this.setState({"ext": value});
}).done();
}
getInitialState() {
return { };
}
saveData(target, value, i) {
if (i === 1){
AsyncStorage.setItem("local", value);
this.setState({local: value});
}
else{
AsyncStorage.setItem("ext", value);
this.setState({ext: value});
}
}
现在正在运作
答案 1 :(得分:0)
好吧我认为你应该这样做:
var count;//declare global variable
saveData(value) {
count++;
AsyncStorage.setItem(""myKey"+count", value);//you can store distinct //key
this.setState({"myKey": value});
}