[React Native] React Native + Node.js + MySQL To do list ๋ง๋ค๊ธฐ 5 - Axios ์ด์ฉํ์ฌ CRUD ๊ตฌํํ๊ธฐ (Implementing a api with Axios)
์๋ ํ์ธ์ Foma ์ ๋๋ค.
์ด๋ฒ ๊ธ์ React Native + Node.js + MySQL ํํ ๋ฆฌ์ผ์ ๋ง์ง๋ง ๋จ๊ณ์ธ Axios๋ฅผ ํ์ฉํ์ฌ CRUD๋ฅผ ๊ตฌํํด ์ค์ ์๋ฒ์ API๋ฅผ ์์ฒญํ๊ณ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ค๊ณ , ์์ฑํ๊ณ , ์์ ํ๊ณ ,์ญ์ ํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์ ๋ฆฌํ๋๋ก ํ๊ฒ ์ต๋๋ค.
๋ฐ๋ก ์์ํ ๊ฒ์~
Tutorial
- React Native + Node.js + MySQL To do list ๋ง๋ค๊ธฐ 1 - ์๋ฒ ์ด๊ธฐ ์ธํ ํ๊ธฐ (Initialize server setting)
- React Native + Node.js + MySQL To do list ๋ง๋ค๊ธฐ 2 - MySQL ํ ์ด๋ธ ๋ง๋ค๊ธฐ (Create a MySQL Table)
- React Native + Node.js + MySQL To do list ๋ง๋ค๊ธฐ 3 - React Native ํ๋ฉด ๊ตฌํํ๊ธฐ (Implement frontend)
- React Native + Node.js + MySQL To do list ๋ง๋ค๊ธฐ 4 - Express๋ก CRUD ๊ตฌํํ๊ธฐ (Building a CRUD API with Express)
- React Native + Node.js + MySQL To do list ๋ง๋ค๊ธฐ 5 - Axios ์ด์ฉํ์ฌ CRUD ๊ตฌํํ๊ธฐ (Implementing a api with Axios)
Install
axios
npm install axios@0.25.0
Implementing a api with Axios
Axios๋ฅผ import ํด์ค๋๋ค.
import Axios from 'axios';
Create
post ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ๋ก์ปฌ ์์ดํผ ์ฃผ์์ /create url๋ก todo ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด ์์ฑํด ์ค๋๋ค.
const addTodo = (todo: TodoModel) => {
Axios.post('http://๋ก์ปฌ์์ดํผ์ฃผ์:ํฌํธ๋ฒํธ/create', todo)
.then(res => {
console.log(res.data);
getTodos()
})
.catch(error => console.log(error));
};
Read
get ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ Todo ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์ต๋๋ค.
const getTodos = () => {
Axios.get('http://๋ก์ปฌ์์ดํผ์ฃผ์:ํฌํธ๋ฒํธ/todos')
.then(res => {
setTodos(res.data);
})
.catch(error => console.log(error));
};
Update
put ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ๋ณ๊ฒฝํ Todo ๋ฐ์ดํฐ๋ฅผ ์ ์กํด ์ค๋๋ค.
const editTodo = (todo: TodoModel) => {
Axios.put('http://๋ก์ปฌ์์ดํผ์ฃผ์:ํฌํธ๋ฒํธ/todos', todo)
.then(res => {
console.log(res.data);
})
.catch(error => console.log(error));
};
Delete
delete ๋ฉ์๋๋ฅผ ์ด์ฉํ์ฌ ์ญ์ ํ id๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฃ์ด ์ ์กํด ์ค๋๋ค.
const deleteTodo = (id: number) => {
Axios.delete(`http://๋ก์ปฌ์์ดํผ์ฃผ์:ํฌํธ๋ฒํธ/todos/${id}`)
.then(res => {
console.log(res.data);
getTodos();
})
.catch(error => console.log(error));
};
CRUD Todos
Get todos
useEffect๋ฅผ ํ์ฉํด ํ๋ฉด์ด ๋ ๋๋ง ๋ ๋ todos๋ฅผ ๋ฐ์์ค๋๋ก ํฉ๋๋ค.
useEffect(() => {
getTodos();
}, []);
์๋์ ๊ฐ์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์๋ Todos๊ฐ ์ฝ์ด์ ธ ์ต๋๋ค.
Add todo
handleSubmit์์ Todo๊ฐ ์ถ๊ฐ ๋์์ ๋ addTodo ๋ฉ์๋์ ํ์ฌ todo ๋ฐ์ดํฐ๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ฃ์ด ์ถ๊ฐํด ์ค๋๋ค.
const handleSubmit = (todo: TodoModel) => {
if (
todo.author == '' ||
todo.title == '' ||
todo.content == '' ||
todo.priority == -1
) {
Alert.alert('Please fill in all the required fields!');
} else {
...
} else {
addTodo(todo);
}
setModalVisible(false);
getTodos();
}
};
์ฐ์ธก ์๋จ์ + ๋ฒํผ์ ๋๋ฅด๊ณ ์ํ๋ ๋ด์ฉ์ ์ ๋ ฅํ๋ฉด ํด๋น Todo๊ฐ ์ถ๊ฐ๋ฉ๋๋ค,
Update todo
handleSubmit์์ Todo๊ฐ ์์ ๋์์ ๋ editTodo์ ๋ณ๊ฒฝํ todo ๋ฐ์ดํฐ๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ด์ ์์ ํด ์ค๋๋ค.
const handleSubmit = (todo: TodoModel) => {
if (
todo.author == '' ||
todo.title == '' ||
todo.content == '' ||
todo.priority == -1
) {
Alert.alert('Please fill in all the required fields!');
} else {
if (editIndex !== -1) {
editTodo(todo);
setEditIndex(-1);
} else {
3๋ฒ์งธ Todo์ ์์ ๋ฒํผ์ ๋๋ฌ author์ ์ด๋ฆ์ Young์ผ๋ก ์์ ํ๋ฉด ์๋์ ๊ฐ์ด Todo๊ฐ ์์ ๋ฉ๋๋ค.
Delete todo
delete ๊ฒฝ๊ณ ์ฐฝ์ด ๋จ๊ณ OK ๋ฒํผ์ด ๋๋ ธ์ ๋ ํ์ฌ index๋ฒ์งธ todo์ id๋ฅผ ํ๋ผ๋ฏธํฐ๋ก ๋ด์ ์ญ์ ํด ์ค๋๋ค.
const showDeleteAlert = (index: number) => {
Alert.alert(
'Delete',
'Do you want to delete?',
[
{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed!'),
},
{
text: 'OK',
onPress: () => {
deleteTodo(todos[index].todoid);
},
},
],
{cancelable: false},
);
};
๊ฒฝ๊ณ ์ฐฝ์ด ๋จ๊ณ OK ๋ฒํผ์ ๋๋ฅด๋ฉด ํด๋น Todo๊ฐ ์ญ์ ๋ฉ๋๋ค.
Source code
์๋ ๋ ํฌ์งํ ๋ฆฌ๋ก ์ด๋ํ๋ฉด ์ ์ฒด ์์ค๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
๊ถ๊ธํ์ ์ ์์ผ์๋ฉด ์ธ์ ๋ ๋๊ธ ๋ฌ์์ฃผ์ธ์!