刚才我开始使用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>
)
}
Run Code Online (Sandbox Code Playgroud)
为此它正常工作,但我想将记录添加到数组中,而不是每次都更改记录.我想将记录推送到一个数组中,需要在视图中显示总数组数据,(即)我还需要以前可用的数据.
pur*_*rii 33
使用JSON.stringify和JSON.parse将数组存储为值via AsyncStorage.
const stringifiedArray = JSON.stringify(somearray)
Run Code Online (Sandbox Code Playgroud)
const restoredArray = JSON.parse(stringifiedArray)
Run Code Online (Sandbox Code Playgroud)
AsyncStorage return AsyncStorage.getItem('somekey')
.then(req => JSON.parse(req))
.then(json => console.log(json))
.catch(error => console.log('error!'));
const someArray = [1,2,3,4];
return AsyncStorage.setItem('somekey', JSON.stringify(someArray))
.then(json => console.log('success!'))
.catch(error => console.log('error!'));
Run Code Online (Sandbox Code Playgroud)
用于设置
AsyncStorage.setItem('name', JSON.stringify(your array));
Run Code Online (Sandbox Code Playgroud)
为了得到
AsyncStorage.getItem('name', (error, result) => {
this.setState({ name: JSON.parse(result) }, function () { });});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18455 次 |
| 最近记录: |