刚才我开始使用 AsyncStorage。我尝试像这样存储输入文本值:
class Sample extends React.Component{
constructor(props){
super(props);
this.state = {
Name:' ',
};
}
componentDidMount() {
AsyncStorage.getItem("Name").then((value) =>{
this.setState({"Name":value})
}).done();
handleChange(e){
AsyncStorage.setItem("Name", e.nativeEvent.text)
this.setState({
email: e.nativeEvent.text
})
}
render(){
return(
<View>
<TextInput
style={styles.textContainer}
value={this.state.Name}
placeholder="Name"
onChange={this.handleChange.bind(this)}/>
</View>
)
}
为此,它工作正常,但我想将记录添加到数组中,而不是每次都更改记录。我想将记录推送到一个数组中,并且需要在视图中显示总数组数据,(即)我还需要以前可用的数据。
原文由 Hussian Shaik 发布,翻译遵循 CC BY-SA 4.0 许可协议
字符串化数组
使用 JSON.stringify 和 JSON.parse 通过
AsyncStorage
将数组存储为值。存储/字符串化
恢复/解析
与
AsyncStorage