Guest User

Untitled

a guest
Apr 21st, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {DELETE_TODO, FETCH_TODOS} from './queries';
  3. import { graphql, Mutation } from 'react-apollo';
  4. import {Button} from 'react-native';
  5.  
  6. // The "update" prop is used to update the cache after the mutation so that the mutations reflect in the UI
  7. const DeleteButton = (props) => {
  8. return (
  9. <Mutation
  10. mutation={DELETE_TODO}
  11. update= {(cache) => {
  12. const data = cache.readQuery({ query: FETCH_TODOS});
  13. cache.writeQuery({
  14. query: FETCH_TODOS,
  15. data: {
  16. ...data,
  17. todos: data.todos.filter((todo) => (props.todo.id !== todo.id))
  18. }
  19. })
  20. }}
  21. >
  22. {(delete_todos, {data}) => (
  23. <Button
  24. title="Delete"
  25. style={props.style}
  26. onPress={() => {
  27. // "delete_todos" function takes a variable object to perform a mutation
  28. delete_todos({
  29. variables: {
  30. todo_id: props.todo.id
  31. }
  32. });
  33. }}
  34. />
  35. )}
  36. </Mutation>
  37. )
  38. }
  39.  
  40. export default DeleteButton;
Add Comment
Please, Sign In to add comment