Skip to content

Instantly share code, notes, and snippets.

@cant89
Last active September 11, 2020 18:56
Show Gist options
  • Save cant89/cde66aa82e41d096a6a21216daf4b7d8 to your computer and use it in GitHub Desktop.
Save cant89/cde66aa82e41d096a6a21216daf4b7d8 to your computer and use it in GitHub Desktop.
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