Skip to content

Instantly share code, notes, and snippets.

@cant89
Last active September 11, 2020 18:56
Show Gist options
  • Save cant89/72bf7f1b806e8d4b8d8dee9103194f47 to your computer and use it in GitHub Desktop.
Save cant89/72bf7f1b806e8d4b8d8dee9103194f47 to your computer and use it in GitHub Desktop.
import { useQuery } from 'react-query';
export const useFetchFirstTodo = () => {
const { data, error, isLoading } = useQuery("fetchFirstTodo", fetchFirstTodo);
// -> dispatch and action
// -> manipulate the response before sending back to component
// -> call another api
// -> whatever makes sense for you to put here...
return { data, error, isLoading }
}
const Todo = () => {
const { data, error, isLoading } = useFetchFirstTodo();
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