04 API와 Axios 1
POST
import React, { useState } from "react";
import axios from "axios";
function Users() {
let [result, setResult] = useState("");
const login = { email: "eve.holt@reqres.in", password: "cityslicka" };
axios
.post("https://reqres.in/api/login", login)
.then((response) => setResult(response.data.token));
return (
<div>
<h4>React Axios로 HTTP POST 요청하기</h4>
<div>Token: {result}</div>
</div>
);
}
export default Users;
GET
import React, { useState, useEffect } from "react";
import axios from "axios";
function Users() {
let [result, setResult] = useState("");
useEffect(() => {
axios
.get("https://reqres.in/api/users/2")
.then((response) => setResult(response.data.data));
}, []);
return (
<div>
<h4>React Axios로 HTTP GET 요청하기</h4>
<div>
{}
Name: {result.first_name + " " + result.last_name} <br />
Email: {result.email} <br />
</div>
</div>
);
}
export default Users;
PUT
import React, { useEffect, useState } from "react";
import axios from "axios";
function Users() {
let [result, setResult] = useState("");
const updateData = {
first_name: "White",
last_name: "Rabbit",
email: "alice@elice.io",
};
useEffect(() => {
axios
.put("https://reqres.in/api/users/2", updateData)
.then((response) => setResult(response.data));
}, []);
return (
<div>
<h4>React Axios로 HTTP PUT 요청하기</h4>
<div>
Name: {result.first_name + " " + result.last_name} <br />
Email: {result.email} <br />
Update Date: {result.updatedAt} <br />
</div>
</div>
);
}
export default Users;
DELETE
import React, { useState } from "react";
import axios from "axios";
function Users() {
let [result, setResult] = useState("");
axios
.delete("https://reqres.in/api/users/2")
.then((response) => setResult(response.status));
return (
<div>
<h4>React Axios로 HTTP DELETE 요청하기</h4>
<div>
{}
Status: {result} <br />
</div>
</div>
);
}
export default Users;