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 from 'react' | |
import { useMutation, useQuery, useQueryClient } from 'react-query' | |
const fetchTodos = async () => { | |
// Get todos list from the API | |
} | |
const markAsDone = async (id) => { | |
// Send a request to the API | |
} |
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 from 'react' | |
const markAsDone = async (todoId) => { | |
// Send a request to mark a todo as done | |
} | |
export default function Todo({ todo }) { | |
const handleMarkAsDone = async (todo) => { | |
// Optimistically update local state to mark this task as done | |
setState(prevState => ({ |
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, useState } from 'react' | |
import Todo from './Todo' | |
const fetchTodos = async () => { | |
// Fetch todos list from API | |
} | |
export default function Todos() { | |
const [state, setState] = useState({ loading: true }) |
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
useMutation(yourMutationFunction, { | |
// Before the mutation starts | |
async onMutate() { | |
/* 1. Stop the queries that may affect this operation */ | |
/* 2. Modify cache to reflect this optimistic update */ | |
/* 3. Return a snapshot so we can rollback in case of failure */ | |
const context = { ... } | |
return context | |
}, |
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 from 'react' | |
import { useQuery } from 'react-query' | |
import LikeTweet from './LikeTweet' | |
const fetchTweets = async () => { | |
// Fetch tweets from the API | |
} | |
function Tweets() { | |
const { data, isError, isLoading } = useQuery('tweets', fetchTweets); |
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 from 'react' | |
import { useMutation, useQuery, useQueryClient } from 'react-query' | |
const toggleLikeTweet = async (tweetId) => { | |
// Send a request to API | |
} | |
function LikeTweet({ tweetId, isLiked }) { | |
const queryClient = useQueryClient() |
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 from 'react' | |
import { useQuery } from 'react-query' | |
const fetchTodos = async () => { | |
// Fetch todos from the API | |
} | |
function Todos() { | |
const { data, isError, isLoading } = useQuery('todos', fetchTodos) |
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 from 'react' | |
import { useMutation, useQueryClient } from 'react-query' | |
const insertTodo = async (todoId) => { | |
// Send a request to API | |
} | |
function InsertTodo() { | |
const [text, setText] = useState('') |
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 from 'react' | |
import { gql, useQuery } from '@apollo/client' | |
const GET_TWEETS = gql` | |
query { | |
tweets { | |
id | |
text | |
is_liked | |
} |
OlderNewer