jasonmerino / react-native-simple-store

A minimalistic wrapper around React Native's AsyncStorage.

Home Page:https://www.npmjs.com/package/react-native-simple-store

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Removing object from array

DZuz14 opened this issue · comments

Not sure if this can be made easier via your module, or if I need to rethink the structure of how I am storing my data, but here is the problem I am having.

A portion of my app allows users to schedule alarms. When they schedule an alarm, it creates an object inside of a currently existing array with key of 'alarms'. Just for an example, let's pretend "alarms" is an empty array right now, and my user clicks the save alarm button. In the code, this is what it does:

    const id = new Date()
    const newAlarm = {
      id: id,
      hour: finalHour,
      minutes: finalMinutes
    }

    store.push('alarms', newAlarm)

This works perfect, and will push as many new alarm objects as the user wants into the array. However, when I give the user the ability to delete the alarm, this is where I am stuck.

To make my example easier to explain, let's pretend that my user created two more alarms after the initial one they just made. "alarms", now is an array with three objects inside of it. Say the user now wants to delete the first object in the array, I have to do something super hacky in order to make this happen.

In the particular view that I have, my React component already has the alarms array saved into local state so I do something like this:

deleteAlarm = id => {
   const alarms = this.state.alarms
   let newAlarms = []

   for(var i = 0; i < alarms.length; i++) {
      let alarm = alarms[i]
      if(alarm.id !== id)
         newAlarms.push(alarm)
   }
  // Can't figure out what to do from here...
}

Basically, I am stumped as to how I could easily remove one object from my existing array, then just update it in order to save the changes. Creating a for loop and a new array with all of the other objects except for the one I want deleted, seems like craziness to me. I was wondering if your module provides an easy way to do so, and I am just unaware of how to do it.

There isn't a built in way to do this right now, but we might be able to work up some new function for the API.

For the time being here's what you could do to update your local state and the simple store value for the alarms key.

deleteAlarm = alarmId => {
  const alarms = this.state.alarms.filter(({ id }) => alarmId !== id)
  this.setState({ alarms });
  // store would be the imported simple store
  store.save('alarms', alarms);
}

Now regarding any changes in the library here, it might be nice to implement an Array.splice method. I'll log an issue to add that method in. In this case here it probably wouldn't be that beneficial, but if you didn't have the values from the store in local component state then it could be really nice to just be able to splice array values from the store.

Thanks for the reply. I agree on the splice idea, that would be pretty cool.