Last active
September 11, 2020 18:56
-
-
Save cant89/cde66aa82e41d096a6a21216daf4b7d8 to your computer and use it in GitHub Desktop.
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 { useQuery } from 'react-query'; | |
const fetchFirstTodo = async () => { | |
try { | |
const res = await fetch("https://jsonplaceholder.typicode.com/todos/1"); | |
const json = await res.json(); | |
return json; | |
} catch (err) { | |
throw err; | |
} | |
}; | |
const Todo = () => { | |
const { data, error, isLoading } = useQuery("fetchFirstTodo", fetchFirstTodo); | |
useEffect(() => { | |
// eventually dispatch redux action | |
// if you need to store something | |
}, [data]); | |
if (error) { | |
return `Error: ${error} `; | |
} | |
if (isLoading) { | |
return "Loading..."; | |
} | |
if (data) { | |
const { title, completed } = data; | |
return `${title} is ${!completed && "not "} completed`; | |
} | |
return null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment