Created
April 7, 2021 13:43
-
-
Save ilxanlar/31f6be8b9d390d3775a18d55f1dd3347 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
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