-
-
Save dineshigdd/1555b0ce5dd74b957799b6b80d138b80 to your computer and use it in GitHub Desktop.
This is the component for deleting a post. It uses GraphQL mutation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from 'react' | |
import { DELETE_POST } from './mutations'; | |
import { useMutation } from '@apollo/client'; | |
export default function deletePost({ post, setAllState , item }) { | |
const [ deletePost, { data, loading , error } ] = useMutation( DELETE_POST); | |
useEffect(()=>{ | |
if( data ){ | |
setAllState( "Post deleted" ) | |
} | |
}, [ data ]); | |
const handleDelete = ()=>{ | |
deletePost( { variables : { id : post.id }}) | |
item.current[ post.id ].remove(); | |
setAllState(''); | |
} | |
//place the return statement after useEffect. | |
//Otherwise there will be an runtime error when submitting the form | |
if (loading) return 'Submitting...'; | |
if (error){ | |
setAllState( error.message); | |
} | |
return ( | |
<div> | |
<button onClick={ handleDelete } type="submit">Delete</button> | |
</div> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment