localbase icon indicating copy to clipboard operation
localbase copied to clipboard

My delete isn't working.

Open abdojulari opened this issue 4 years ago • 1 comments

I don't know why when I click to delete the item, nothing happens, so when I refresh the page, the item will now be deleted. And when I delete second time and refresh, the first Item will be restored. I want to delete progressively without refresh the page. Thank you `import React, { useState, useEffect } from 'react' import notifications from '../mock/notifications.json' import Localbase from 'localbase'

export default function Notification() { const [data, setData] = useState([])

let db = new Localbase('db');
db.config.debug = false
notifications.map((item, index) => {
    return db.collection('notification').add({
         id: item.id, 
         excerpt: item.excerpt,
         content: item.content, 
         date: item.date

}, `my-key${index}`)
})


useEffect(  () => {
      function getData(){
     db.collection('notification').get().then(response => {
            console.log(response); 
            setData(response)
        })
        .catch(error => {
            console.log(error)
        });
 }    
 getData();
}, [])

function onDelete(index){
    db.collection('notification')
      .doc(`my-key${index}`)
      .delete()
      .then(response => {
          console.log(response)
      })
      .catch(error => {
          console.log(error)
      })
} 
return (
    <>
    <ul>
        { data.map((item, index) => 
           (
        <li key={index} >
           <div>
              <h4>{item.date}</h4>
              <p>{item.excerpt}</p>
              <button onClick={()=> 
                    onDelete(index)
               }
              >Delete</button>
          </div>
          </li>
        ) )}
    </ul> 
    </>
)

}`

abdojulari avatar Feb 15 '21 00:02 abdojulari

You are deleting the item from the database but you don't fetch and get a collection again. Changing it in the database won't mean it has changed in your component.

You could delete it from the database and then remove it from an object array so that way your component AND the database. Another way is to delete it from the database and just call the 'db.collection.xxxxxxx.get()' again

teamsuperpanda avatar Sep 12 '21 11:09 teamsuperpanda