[TS][ํ”„๋กœ์ ํŠธ] Country Card๐Ÿ—บ๏ธ - API ํ˜ธ์ถœ ๋กœ์ง, useState๋ฅผ ์ด์šฉํ•œ ์ƒํƒœ ๊ด€๋ฆฌ TypeScript๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ

์šฐ์—ฅยท2024๋…„ 6์›” 27์ผ

ย 

๐Ÿ”— ๊นƒํ—ˆ๋ธŒ : https://github.com/Park-Yuyeong/account-book
โœˆ๏ธ ์›น์‚ฌ์ดํŠธ : https://country-card-sooty.vercel.app

ย 

๐Ÿ› ๏ธ Stacks

ย 

โ˜๏ธ Country API

class CountryAPI {
  private client: AxiosInstance;

  constructor(client: AxiosInstance) {
    this.client = client;
  }

  async getCountries() {
    try {
      const path = "/all";
      const response = await this.client.get<TCountry[]>(path);
      const result = response.data;

      return result;
    } catch (error) {
      console.error("Country API Error => ", error);
    }
  }
}
  • GET ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•œ API ํ˜ธ์ถœ

ย 

โ˜๏ธ useState๋ฅผ ์ด์šฉํ•œ ์ƒํƒœ ๊ด€๋ฆฌ

  const [countries, setCountries] = useState<TCountry[]>([]);
  
  const changeCountryCardState = (country: TCountry) => {
    const newCountries = countries.map((prevCountry) =>
      prevCountry.name.common === country.name.common
        ? { ...prevCountry, selected: !prevCountry.selected }
        : prevCountry
    );

    setCountries(newCountries);
  };
  • ์ƒํƒœ ๋ณ€ํ™”์— ๋”ฐ๋ผ selected ์†์„ฑ์ด ๋ณ€ํ•˜๋„๋ก ๊ตฌํ˜„
  • selected ๊ฐ’์ด true๋ฉด Favorite Countries์— ๋‚˜๋ผ ์นด๋“œ ์œ„์น˜
  • selected ๊ฐ’์ด false๋ฉด Countries์— ๋‚˜๋ผ ์นด๋“œ ์œ„์น˜
profile
๐ŸŒธ๐Ÿ˜Š๐ŸŒธ

0๊ฐœ์˜ ๋Œ“๊ธ€