Last active
September 28, 2024 06:40
-
-
Save bryanmylee/f078c2be13c225617354f1b65e7e1b4c to your computer and use it in GitHub Desktop.
Data loading skeleton best practices
This file contains hidden or 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
| function App() { | |
| const itemId: string | null = /* --snip-- */; | |
| return <Item id={itemId} />; | |
| } | |
| interface ItemProps { | |
| id: string | null; | |
| } | |
| function Item({ id }: ItemProps) { | |
| if (id == null) { | |
| return <ItemView item={null} />; | |
| } | |
| return <ItemQuery id={id} />; | |
| } | |
| interface ItemViewProps { | |
| item: Item | null; | |
| } | |
| function ItemView({ item }: ItemViewProps) { | |
| return ( | |
| <div> | |
| <p>{item.name ?? <Skeleton />}</p> | |
| <p>{item.description ?? <Skeleton count={3} />}</p> | |
| </div> | |
| ); | |
| } | |
| interface ItemQueryProps { | |
| id: string; | |
| } | |
| function ItemQuery({ id }) { | |
| const query = useQuery(/* --snip-- */); | |
| // other expensive hooks | |
| return <ItemView item={query.data ?? null} />; | |
| } |
This file contains hidden or 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
| function App() { | |
| const query = useQuery(/* --snip-- */); | |
| return ( | |
| <ul> | |
| {query.isLoading ? ( | |
| range(5).map(() => <ListItem item={null} />) | |
| ) : ( | |
| query.data.map((item) => <ListItem item={item} />) | |
| )} | |
| </ul> | |
| ); | |
| } | |
| interface ListItemProps { | |
| item: Item | null; | |
| } | |
| function ListItem({ item }: ListItemProps) { | |
| return ( | |
| <div> | |
| <p>{item.name ?? <Skeleton />}</p> | |
| <p>{item.description ?? <Skeleton count={3} />}</p> | |
| </div> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment