Right way to setItem in AsyncStorage

前端 未结 4 1017
萌比男神i
萌比男神i 2021-02-15 16:57

I want to use AsyncStorage.setItem inside AsyncStorage.getItem. How to do that in right way?

My code is as follows:

createVehic         


        
4条回答
  •  隐瞒了意图╮
    2021-02-15 17:17

    import React, { Component } from 'react'
    import { StatusBar } from 'react-native'
    import { AsyncStorage, Text, View, TextInput, StyleSheet } from 'react-native'
    class AsyncStorageExample extends Component {
       state = {
          'name': ''
       }
       componentDidMount = () => AsyncStorage.getItem('name').then((value) => this.setState({ 
    'name': value }))
       setName = (value) => {
          AsyncStorage.setItem('name', value);
          this.setState({ 'name': value });
       }
       render() {
          return (
         
            
            
               {this.state.name}
            
         
          )
       }
    }
    export default AsyncStorageExample
    const styles = StyleSheet.create ({
       container: {
          flex: 1,
          alignItems: 'center',
          marginTop: 50
       },
       textInput: {
          margin: 5,
          height: 100,
          borderWidth: 1,
          backgroundColor: '#7685ed'
       }
    })
    

提交回复
热议问题