We want to hear from you!Take our 2021 Community Survey!
Try out a preview of the new React Docs!๐Ÿ‘‰ beta.reactjs.org

AJAX ์™€ APIs

์–ด๋–ป๊ฒŒ AJAX ํ˜ธ์ถœ์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ์š”?

์„ ํ˜ธํ•˜๋Š” AJAX ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ React์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ ๋ช…ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ๋Š” Axios, jQuery AJAX, ๊ทธ๋ฆฌ๊ณ  ๋ธŒ๋ผ์šฐ์ €์— ๋‚ด์žฅ๋œ window.fetch ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ปดํฌ๋„ŒํŠธ์˜ ์ƒ๋ช…์ฃผ๊ธฐ ์ค‘ ์–ด๋””์—์„œ AJAX ํ˜ธ์ถœ์„ ํ•  ์ˆ˜ ์žˆ๋‚˜์š”?

AJAX ํ˜ธ์ถœ์„ ํ†ตํ•œ ๋ฐ์ดํ„ฐ๋Š” ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ ์ค‘ componentDidMount ์•ˆ์— ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„ ์˜ฌ ๋•Œ setState๋ฅผ ํ†ตํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค.

์˜ˆ์‹œ: ๋กœ์ปฌ state๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด AJAX ๊ฒฐ๊ณผ ์‚ฌ์šฉํ•˜๊ธฐ

์•„๋ž˜ ์ปดํฌ๋„ŒํŠธ๋Š” ๋กœ์ปฌ ์ปดํฌ๋„ŒํŠธ์˜ state๋ฅผ ์ฑ„์šฐ๊ธฐ ์œ„ํ•˜์—ฌ componentDidMount ์•ˆ์—์„œ ์–ด๋–ป๊ฒŒ AJAX ํ˜ธ์ถœ์„ ๋งŒ๋“œ๋Š”์ง€ ๋ณด์—ฌ ์ค๋‹ˆ๋‹ค.

API ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ JSON ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

{
  "items": [
    { "id": 1, "name": "Apples",  "price": "$2" },
    { "id": 2, "name": "Peaches", "price": "$5" }
  ]
}
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      error: null,
      isLoaded: false,
      items: []
    };
  }

  componentDidMount() {
    fetch("https://api.example.com/items")
      .then(res => res.json())
      .then(
        (result) => {
          this.setState({
            isLoaded: true,
            items: result.items
          });
        },
        // ์ฃผ์˜: ์ปดํฌ๋„ŒํŠธ์— ์žˆ๋Š” ์‹ค์ œ ๋ฒ„๊ทธ๋กœ ์ธํ•ด ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋ฅผ
        // ๋†“์น˜์ง€ ์•Š๊ณ  ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”
        // catch() ๋ธ”๋ก๋ณด๋‹ค๋Š” ์—ฌ๊ธฐ์„œ ์—๋Ÿฌ๋ฅผ ๋‹ค๋ค„์ฃผ๋Š” ๊ฒŒ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.
        (error) => {
          this.setState({
            isLoaded: true,
            error
          });
        }
      )
  }

  render() {
    const { error, isLoaded, items } = this.state;
    if (error) {
      return <div>Error: {error.message}</div>;
    } else if (!isLoaded) {
      return <div>Loading...</div>;
    } else {
      return (
        <ul>
          {items.map(item => (
            <li key={item.id}>
              {item.name} {item.price}
            </li>
          ))}
        </ul>
      );
    }
  }
}

์ด๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ Hook์œผ๋กœ ์ž‘์„ฑํ•œ ์ฝ”๋“œ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

function MyComponent() {
  const [error, setError] = useState(null);
  const [isLoaded, setIsLoaded] = useState(false);
  const [items, setItems] = useState([]);

  // ์ฃผ์˜: ๋นˆ deps ๋ฐฐ์—ด []์€
  // useEffect๊ฐ€ componentDidMount()์ฒ˜๋Ÿผ
  // ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋˜๋Š” ๊ฑธ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  useEffect(() => {
    fetch("https://api.example.com/items")
      .then(res => res.json())
      .then(
        (result) => {
          setIsLoaded(true);
          setItems(result);
        },
        // ์ฃผ์˜: ์ปดํฌ๋„ŒํŠธ์— ์žˆ๋Š” ์‹ค์ œ ๋ฒ„๊ทธ๋กœ ์ธํ•ด ๋ฐœ์ƒํ•œ ์˜ˆ์™ธ๋ฅผ
        // ๋†“์น˜์ง€ ์•Š๊ณ  ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”
        // catch() ๋ธ”๋ก๋ณด๋‹ค๋Š” ์—ฌ๊ธฐ์„œ ์—๋Ÿฌ๋ฅผ ๋‹ค๋ค„์ฃผ๋Š” ๊ฒŒ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.
        (error) => {
          setIsLoaded(true);
          setError(error);
        }
      )
  }, [])

  if (error) {
    return <div>Error: {error.message}</div>;
  } else if (!isLoaded) {
    return <div>Loading...</div>;
  } else {
    return (
      <ul>
        {items.map(item => (
          <li key={item.id}>
            {item.name} {item.price}
          </li>
        ))}
      </ul>
    );
  }
}
Is this page useful?Edit this page