CRYPTO TRACKER - (4)Home part two

Apeachicetea·2021년 12월 13일
0

CRYPTO TRACKER

목록 보기
4/4

Coin API 받아오기

//coins.tsx
useEffect(() => {
    (async() => {
      const response = await fetch("https://api.coinpaprika.com/v1/coins");
      const json = await response.json();
      // console.log(json);
      setCoins(json.slice(0, 100));
      setLoading(false);
    })();
  }, [])

Link를 통해 데이터 전달하기

useLocation()을 콘솔로 찍어보면 다음과 같이 나오게 된다.

//coins.tsx
<Coin key={el.id}>
  <Link 
    to={{
      pathname: `/${el.id}`,
      state: { name: el.name }
    }}

//react-router-dom 6.0.0이상부터는
//<Link to={} state={} > 처럼 사용한다.

>


//coin.tsx
interface RouteState {
  name: string;
}

function Coin() {
  const { coinId } = useParams<RouteParams>();
  const [loading, setLoading] = useState(true);
  const { state } = useLocation<RouteState>();
  console.log(state.name);

  return (
    <Container>
    <Header>
      <Title>{ state?.name || "Loading" }</Title>
    </Header>
    {
      loading
      ? <Loader>Loading...</Loader>
      : null
    }
    </Container>
  )
}

profile
웹 프론트엔드 개발자

1개의 댓글

comment-user-thumbnail
2024년 7월 16일

A colleague from New Zealand recommended this site to me, and I decided to give it a shot. I’ve been pleasantly surprised by the extensive selection of games and the user-friendly layout. It’s a perfect way to relax and have fun after a long day. The security features give me peace of mind while playing. If you’re looking for a great online casino experience, you might want to explore Galaxyno Casino and see what it has to offer.

답글 달기

관련 채용 정보