Last active
September 13, 2022 00:59
-
-
Save silesky/50fff2efe92edfd1424f63fd66a3b9e6 to your computer and use it in GitHub Desktop.
Loader pattern for intermediate state
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 from "react"; | |
import Todo from "./Todo"; | |
import useTodosLoading from "./useTodosLoading"; | |
import React from "react"; | |
const Loader = ({ isLoading, errorMessage, children }) => { | |
if (errorMessage) return <Error errrorMessage={errorMessage} />; | |
if (isLoading) return <Loading />; | |
return children; | |
}; | |
const TodoList = () => { | |
const { isError, todos } = useFetchTodos(); | |
return ( | |
<Loader isLoading={!todos.length} errorMessage={isError ? 'Could not load todos.' : undefined} > | |
{todos.map((todo) => ( | |
<Todo {...todo} /> | |
))} | |
</Loader> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment