Skip to content

Instantly share code, notes, and snippets.

@cant89
Last active September 6, 2020 10:10
Show Gist options
  • Save cant89/8a0a2d21bfecf23d550883475b63f3ad to your computer and use it in GitHub Desktop.
Save cant89/8a0a2d21bfecf23d550883475b63f3ad to your computer and use it in GitHub Desktop.
const Todo = () => {
const [loading, setLoading] = useState(true);
const [error, setError] = useState();
const [data, setData] = useState();
const fetchTodo = useCallback(async () => {
try {
const res = await fetch("https://jsonplaceholder.typicode.com/todos/1");
const json = await res.json();
setData(json);
} catch (err) {
setError(err);
}
setLoading(false);
}, []);
useEffect(() => {
setLoading(true);
fetchTodo();
}, [fetchTodo]);
useEffect(() => {
/*
eventually dispatch a redux action here
if you need to save something in the store
*/
}, [data]);
if (error) {
return `Error: ${error} `;
}
if (loading) {
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