Last active
September 6, 2020 10:10
-
-
Save cant89/8a0a2d21bfecf23d550883475b63f3ad 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
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