Skip to content

Instantly share code, notes, and snippets.

@ilxanlar
Created April 7, 2021 13:43
Show Gist options
  • Save ilxanlar/31f6be8b9d390d3775a18d55f1dd3347 to your computer and use it in GitHub Desktop.
Save ilxanlar/31f6be8b9d390d3775a18d55f1dd3347 to your computer and use it in GitHub Desktop.
import React, { useEffect, useState } from 'react'
import Todo from './Todo'
const fetchTodos = async () => {
// Fetch todos list from API
}
export default function Todos() {
const [state, setState] = useState({ loading: true })
const { error, loading, todos } = state
useEffect(() => {
// Load todos list
async function fetchMyTodos() {
try {
const todos = await fetchTodos()
setState({ todos })
} catch (error) {
setState({ error: error.message })
}
}
fetchMyTodos()
}, [])
if (error) {
return (
<p>{error}</p>
)
}
if (loading) {
return (
<p>Loading todos...</p>
)
}
return (
<ul>
{todos.map(todo => (
<Todo key={todo.id} todo={todo} />
))}
</ul>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment