持久化存储 AsyncStorage

迁移到社区了 react-native-community/react-native-async-storage

官方文档 asyncstorage

yarn add react-native-community/react-native-async-storage

ios:
cd ios/ && pod install
android  build 的时候 自动link

Store data

storeData = async () => {
  try {
    await AsyncStorage.setItem('@storage_Key', 'stored value')
  } catch (e) {
    // saving error
  }
}

Read data

getData = async () => {
  try {
    const value = await AsyncStorage.getItem('@storage_Key')
    if(value !== null) {
      // value previously stored
    }
  } catch(e) {
    // error reading value
  }
}

Remove data

removeValue = async () => {
  try {
    await AsyncStorage.removeItem('@MyApp_key')
  } catch(e) {
    // remove error
  }

  console.log('Done.')
}

更多请参考 更多api

results matching ""

    No results matching ""