我正在使用react native来构建应用程序,我遇到的唯一问题是我有一个跟踪用户进度的进度条但是当我完全关闭应用程序并打开它时,所有内容都重置为原始状态数据。我如何制作它以便在关闭应用程序时保留数据?
不确定如何添加AsyncStorage 这是我的代码:
n = 3;
%// assumes first two values are already set
while ~(result1 {n-1,1}(1,1) - result1 {n-2,1}(1,1) <= 0.1)
%// do something (series of operations in which result1 {n,1} is set)
all_values{n} = [result1; result2];
n = n + 1;
end
答案 0 :(得分:3)
你可以尝试看一下Nic Raboy的这篇文章。它有一个视频演示以及示例代码。
https://blog.nraboy.com/2015/09/saving-data-in-your-react-native-mobile-application/
以下是一些示例代码(来自文章),演示了如何使用异步存储保存数据。干杯!
"use strict";
var React = require("react-native");
var {
AppRegistry,
StyleSheet,
Text,
View,
TextInput,
AsyncStorage,
} = React;
var ReactProject = React.createClass({
componentDidMount: function() {
AsyncStorage.getItem("myKey").then((value) => {
this.setState({"myKey": value});
}).done();
},
getInitialState: function() {
return { };
},
render: function() {
return (
<View style={styles.container}>
<Text style={styles.saved}>
{this.state.myKey}
</Text>
<View>
<TextInput
style={styles.formInput}
onChangeText={(text) => this.saveData(text)}
value="" />
</View>
<Text style={styles.instructions}>
Type something into the text box. It will be saved to
device storage. Next time you open the application, the saved data
will still exist.
</Text>
</View>
);
},
saveData: function(value) {
AsyncStorage.setItem("myKey", value);
this.setState({"myKey": value});
}
});
var styles = StyleSheet.create({
container: {
padding: 30,
flex: 1,
justifyContent: "center",
alignItems: "stretch",
backgroundColor: "#F5FCFF",
},
formInput: {
flex: 1,
height: 26,
fontSize: 13,
borderWidth: 1,
borderColor: "#555555",
},
saved: {
fontSize: 20,
textAlign: "center",
margin: 10,
},
instructions: {
textAlign: "center",
color: "#333333",
marginBottom: 5,
marginTop: 5,
},
});
AppRegistry.registerComponent('ReactProject', () => ReactProject);