简体   繁体   English

在React-native中扩展并显示异步存储中的数组

[英]Extending and displaying an array in asyncstorage in React-native

I'm trying to add user-input to a specific array inside AsyncStorage. 我正在尝试将用户输入添加到AsyncStorage内的特定数组中。 I am not sure if my current code is doing this. 我不确定我当前的代码是否正在执行此操作。 I can't display it via "alert()" and currently I am receiving this error : the bind value at index 1 is null" 我无法通过“ alert()”显示它,当前我收到此错误: the bind value at index 1 is null"

async addAvailableActivity(){
  try {
    AsyncStorage.getItem('availableActivities')
    .then(activities => {this.availableActivities = JSON.parse(activities)})
  } catch (error) {
    alert('Couldnt load activities!')
  }

  let newActivity = this.state.newActivity
  this.availableActivities.push(newActivity)

  try {
    AsyncStorage.setItem('availableActivities', JSON.stringify(this.availableActivities))
  } catch (error) {
    alert('Couldnt add activity!')
  }

}

async displayAvailableActivities(){
  AsyncStorage.getItem(this.availableActivities).then(activities => {alert(activities)})    
}

There are a few problems with your code, first I would store availableActivities in your component state if you want to scope it like that. 您的代码存在一些问题,首先,如果您希望像这样对它进行范围划分,我将在组件状态中存储availableActivities Second, when you call a function async you should be using await for your promises, third and most important AsyncStorage.getItem(this.availableActivities) this seems like a typo, but AsyncStorage key value pairs must be strings. 其次,当您调用async函数时,您应该使用await来实现承诺,第三个也是最重要的AsyncStorage.getItem(this.availableActivities)看起来像是一个错字,但是AsyncStorage键值对必须是字符串。

Here is an update to your code that I think will provide the functionality you're looking for: 这是您的代码的更新,我认为它将提供您正在寻找的功能:

addAvailableActivity = async () => {
  try {
    let activities = await AsyncStorage.getItem('availableActivities')
    let { newActivity } = this.state
    if (activities != null) {
      activities = JSON.parse(activities)
      activities.push(newActivity)
      await AsyncStorage.setItem( 'availableActivities', JSON.stringify(activities) )
    }
  } catch (error) {
    console.warn(error)
  }
}

displayAvailableActivities = () => {
  let activities = await AsyncStorage.getItem('availableActivities')
  console.warn(JSON.parse(activities))
}

note that I changed the function declarations, this isn't necessary but it prevents them from having to be bound in your constructor. 请注意,我更改了函数声明,这不是必需的,但可以防止将它们绑定到构造函数中。 async / await is an alternate syntax to .then which is much cleaner and easier to read. async / await.then的替代语法.then它更.then ,更易于阅读。 To check if AsyncStorage returned a value it's best to test it against null and finally, I fixed the typo in your last function. 要检查AsyncStorage是否返回值,最好针对null进行测试,最后,我在最后一个函数中修复了错字。 Let me know if you have any questions or if I missed the mark 让我知道您是否有任何疑问或我错过了标记

This line 这条线

AsyncStorage.getItem(this.availableActivities).then(activities => {alert(activities)})

Should be 应该

AsyncStorage.getItem('availableActivities').then(activities => {alert(activities)})

I think the problem is that you are using an AsyncStorage which cause Async in this way. 我认为问题是您正在使用AsyncStorage,从而以这种方式导致Async。 I think in this way your code will work: 我认为您的代码将以这种方式工作:

async addAvailableActivity(){
  try {
    AsyncStorage.getItem('availableActivities')
    .then(activities => {this.availableActivities = JSON.parse(activities)
    let newActivity = this.state.newActivity
    this.availableActivities.push(newActivity)
   })
  } catch (error) {
    alert('Couldnt load activities!')
  }

  try {
    AsyncStorage.setItem('availableActivities', JSON.stringify(this.availableActivities))
  } catch (error) {
    alert('Couldnt add activity!')
  }
} 

You also need to use it not exactly after this function. 您还需要不完全在此功能之后使用它。 Use it some where else. 在其他地方使用它。

 async displayAvailableActivities(){
      AsyncStorage.getItem(this.availableActivities).then(activities => {alert(activities)})    
    }

Suggestion: You can also maintain all your states using Redux. 建议:您还可以使用Redux维护所有状态。 You can also update and access your states from any screen that you want. 您还可以从所需的任何屏幕更新和访问状态。 Here is a good tutorial to help you start it: https://www.valentinog.com/blog/redux/ 这是一个很好的教程来帮助您启动它: https : //www.valentinog.com/blog/redux/

Looks like you are trying to get an item in incorrect way. 看来您正在尝试以错误的方式获取商品。

async displayAvailableActivities(){
 AsyncStorage.getItem(this.availableActivities).then(activities => {alert(activities)}) 
}

First param for getItem should be string : getItem的第一个参数应为string

async displayAvailableActivities(){
 AsyncStorage.getItem('availableActivities').then(activities => {alert(activities)})    
}

AsyncStorage getItem AsyncStorage getItem

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM