May 28, 2023
Table of Contents
Reactでtodoアプリの作成でdbを作成したり裏側の処理を実装するのが、面倒だったのでjson-serverを使用することにした。
タスクの追加時のjson-serverでのpostを実装
fetch('http://localhost:3001/tasks', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(newTask)
}).then((response) => {
return response.json();
}).then(data => {
console.log('Success:', data);
const newTasks = [
...tasks,
data
];
setTasks(newTasks);
setIsNewTask(false);
}).catch((error) => {
console.error('Error:', error);
});