Skip to content

Instantly share code, notes, and snippets.

View ilxanlar's full-sized avatar
🍵
Fresh tea!

Mehdi Namvar ilxanlar

🍵
Fresh tea!
View GitHub Profile
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
}
import React, { useState } from 'react'
const toggleLikeTweet = async (tweetId: number) => {
// Send a request to API
}
function LikeTweet({ tweetId, tweetIsLiked }) {
const [isLiked, setIsLiked] = useState(tweetIsLiked)
useEffect(
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 => ({
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 })
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
},
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);
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()
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)
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('')
import React from 'react'
import { gql, useQuery } from '@apollo/client'
const GET_TWEETS = gql`
query {
tweets {
id
text
is_liked
}