Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My delete isn't working. #24

Open
abdojulari opened this issue Feb 15, 2021 · 1 comment
Open

My delete isn't working. #24

abdojulari opened this issue Feb 15, 2021 · 1 comment

Comments

@abdojulari
Copy link

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> 
    </>
)

}`

@teamsuperpanda
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants