React.js(8) - FetchGET&POST
π©π» FetchGET
http://example.com?a=1&b=2
- url ? λ€μ νλΌλ―Έν°=κ° $νλΌλ―Έν°2=κ° ννλ‘ νμν λ°μ΄ν°λ₯Ό μ λ¬νλ€.
- μ£Όλ‘ λ°μ΄ν° μ‘°νλ κ²μ λ±μ κΈ°λ₯μ μ¬μ©λ¨
- λ°μ΄ν°λ₯Ό μ‘°νν΄ κ°μ Έμ μ¬μ©νκΈ° μν λ©μλ
β FetchGet.js
import React, {Component} from 'react';
class R059_FetchGet extends Component {
componentDidMount= async ()=>{
const response = await fetch('http://date.jsontest.com/');
const body = await response.json();
alert(body.date);
}
render() {
return (
<h1>fetch get</h1>
)
}
}
export default R059_FetchGet;
πasync- await λ©μλ
β νν
componentDidMount= async ()=>{
const response = await fetch('http://date.jsontest.com/');
const body = await response.json();
alert(body.date);
}
- JavaScriptμμ λΉλκΈ°μ μΈ μ²λ¦¬λ₯Ό κ°κ²°νκ² μμ±ν μ μλλ‘ νλ λ©μλ
- Promise κ°μ²΄λ₯Ό λ°ννλ ν¨μλ₯Ό λ§μΉ λκΈ°μ μΈ μ½λμ²λΌ μμ±ν μ μλ€.(μ¬μ€ λΉλκΈ°μ μ)
- async ν€μλλ‘ ν¨μλ₯Ό μ μΈνλ©°, ν¨μ λ΄μμ await ν€μλλ₯Ό μ¬μ©νμ¬ Promise κ°μ²΄λ₯Ό μ²λ¦¬νλ€.
- await ν€μλλ Promise κ°μ²΄κ° μ²λ¦¬λ λκΉμ§ μ½λμ μ€νμ μΌμ μ€μ§νκ³ , μ²λ¦¬ κ²°κ³Όλ₯Ό λ°ννλ€.
- async-await ν¨μλ try-catch ꡬ문과 ν¨κ» μ¬μ©νμ¬ λΉλκΈ°μ μΈ μ²λ¦¬ μ€ λ°μν μ μλ μ€λ₯λ₯Ό μ²λ¦¬ν μ μλ€.
- response.json() μ€ date νμ λ°μ΄ν°λ€λ§ alert ν¨μλ‘ λ°νν΄μ€λ€.
π€ try - catch
async function fetchExample() {
try {
const response = await fetch('https://example.com/data');
const data = await response.json();
return data;
} catch (error) {
console.error(error);
}
}
- fetch() ν¨μλ json() λ©μλμμ μ€λ₯κ° λ°μν κ²½μ°, catch ꡬ문μμ μ€λ₯λ₯Ό μ²λ¦¬νκ³ μλ¬ λ©μμ§λ₯Ό μ½μμ μΆλ ₯ν΄μ€λ€.
π try-catch
- μμΈ μ²λ¦¬λ₯Ό ν λ μ¬μ©νλ ꡬ문
- try λΈλ‘μλ μμΈκ° λ°μν κ°λ₯μ±μ΄ μλ μ½λλ₯Ό μμ±νκ³ , catch λΈλ‘μλ μμΈκ° λ°μνμ λ μ²λ¦¬ν μ½λλ₯Ό μμ±νλ€.
try {
const result = 10 / 0; // 0μΌλ‘ λλκΈ°
console.log(result);
} catch (error) {
console.error(error);
}
- 10μ 0μΌλ‘ λλλ μ½λλ₯Ό μμ±νλλ°, μ΄λ μ ν¨νμ§ μμ κ³μ°μ΄λ―λ‘ μμΈκ° λ°μνλ€. μ΄λ catch λΈλ‘μμλ μμΈλ₯Ό μ²λ¦¬νκ³ μλ¬ λ©μμ§λ₯Ό μΆλ ₯νκ² λ¨
π νμΌ μ½μ΄μ€λ μ½λ μμ μμ±
try {
const data = readFile('data.txt'); // νμΌ μ½κΈ°
console.log(data);
} catch (error) {
if (error.code === 'ENOENT') {
console.error('File not found'); // νμΌμ΄ μλ κ²½μ°
} else {
console.error(error); // κΈ°ν μ€λ₯
}
}
π Promise κ°μ²΄
- λΉλκΈ°μ²λ¦¬λ₯Ό μν κ°μ²΄
- λΉλκΈ° μμ μ΄ μλ£λ λκΉμ§ κΈ°λ€λ¦¬μ§ μκ³ λ€μ μμ μ μ²λ¦¬ν μ μλλ‘ ν΄μ€λ€.
π Promise κ°μ²΄ μν
- λκΈ°(pending) μν: λΉλκΈ° μμ μ΄ μμ§ μλ£λμ§ μμ μν
- μ΄ν(fulfilled) μν: λΉλκΈ° μμ μ΄ μ±κ³΅μ μΌλ‘ μλ£λ μνμ΄λ©°, μ΄λ κ²°κ³Ό κ°μ΄ μ λ¬λλ€.
- κ±°λΆ(rejected) μν: λΉλκΈ° μμ μ΄ μ€ν¨ν μνμ λλ€. μ΄λ μ€λ₯ μ λ³΄κ° μ λ¬λ¨
π fetch
const response = await fetch('http://date.jsontest.com/');
- HTTP μμ²μ 보λ΄κ³ μλ΅μ λ°ννλ€.
- APIμμ λ°μ΄ν°λ₯Ό κ°μ Έμμ λ°νν΄μ€λ€.
- awaitλ‘ fetchμ λ°νκ²°κ³Όλ₯Ό κΈ°λ€λ¦°λ€.
π response.json()
const body = await response.json();
- μ΄ν response.json() λ©μλλ₯Ό μ¬μ©νμ¬ μλ΅μ JSON ννλ‘ λ³νν¨
π FetchPOST
- url λ€μ νλΌλ―Έν°λ₯Ό νμνμ§ μκ³ μ¬μ©ν¨(http bodyμ νλΌλ―Έν°λ₯Ό λ£μ§ μλλ€.)
- μλ²μ μνλ λ°μ΄ν°λ₯Ό λ³κ²½νλ λ±μ μν μμ μ μ¬μ©λλ€.
β FetchPost.js
import React from "react";
import { Component } from "react";
class R060_FetchPost extends Component {
componentDidMount=async ()=>{
const response = awiait fetch('http://data.jsontest.com/', {
#fetch λ°©λ²μ getκ³Ό λΉμ·νμ§λ§ λ λ²μ§Έ νλΌλ―Έν°μ post νΈμΆμ λν μ λ³΄κ° μΆκ° λλ€.
#Content-Type λ³μ : json ννμ λ°μ΄ν°λ₯Ό μ¬μ©νκΈ° μν΄ application/jsonμ ν λΉνλ€.
method : 'POST',
headers : {
'Content-Type' : 'application/json',
},
body : {a:"react", b:200},
});
const body = await response.json();
alert(body.date)
}
render () {
return (
<h1>fetch post</h1>
)
}
}
export default R060_FetchPost;