Skip to content

Instantly share code, notes, and snippets.

@jth0024
Created May 17, 2022 15:22
Show Gist options
  • Save jth0024/bba85ae2d810008ba484a9387ca9aea2 to your computer and use it in GitHub Desktop.
Save jth0024/bba85ae2d810008ba484a9387ca9aea2 to your computer and use it in GitHub Desktop.
A component that accepts dependencies as a prop to pass to its "component hook"
import React from 'react';
import { charactersApi } from '../apis';
import { useCharacterList, UseCharacterListDeps } from './useCharacterList';
import {
Button,
Wrapper,
List,
ListItem,
ListItemInfo,
NameWrapper,
IsJediWrapper,
HeaderTypography,
Spinner,
} from './styles';
interface CharacterListProps {
deps?: UseCharacterListDeps
}
const defaultDeps = {
charactersApi,
}
export const CharacterList = ({ deps = defaultDeps }) => {
const [data, handlers] = useCharacterList(deps);
const { characters, loading } = data;
const { onRefresh } = handlers;
return (
<Wrapper>
Star Wars Characters
<Button onClick={onRefresh}>Refresh List</Button>
{loading ? <Spinner /> : (
<List>
<ListItem>
<NameWrapper>
<HeaderTypography>
Name
</HeaderTypography>
</NameWrapper>
<ListItemInfo>
<HeaderTypography>
Age
</HeaderTypography>
</ListItemInfo>
<IsJediWrapper>
<HeaderTypography>
isJedi
</HeaderTypography>
</IsJediWrapper>
</ListItem>
{characters.map((c) => (
<ListItem key={c.name}>
<NameWrapper>
{c.name}
</NameWrapper>
<ListItemInfo>
{c.age}
</ListItemInfo>
<IsJediWrapper>
{c.isJedi ? 'True' : 'False'}
</IsJediWrapper>
</ListItem>
))}
</List>
)}
</Wrapper>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment