useEffect(() => {
setIsLoading(true);
fetch("url.../meetups.json")
.then((response) => {
return response.json();
})
.then((data) => {
const meetups = [];
for (const key in data) {
const meetup = {
id: key,
...data[key],
};
meetups.push(meetup)
}
setIsLoading(false);
setLoadedMeetups(meetups);
});
}, []);
.then((data) => {
const meetups = [];
for (const key in data) {
const meetup = {
id: key,
...data[key],
};
meetups.push(meetup)
}
setIsLoading(false);
setLoadedMeetups(meetups);
})
const addMeetupHandler = (meetupData) => {
fetch("url.../meetups.json", {
method: "POST",
body: JSON.stringify(meetupData),
headers: {
'Content-type': 'application/json'
}
})
.then((res) => res.json())
.then((data) => setLoadedMeetups((prev) => [...prev, { id: data.name, ...meetupData }]))
.then(() => {
history.replace('/');
})
};
<NewMeetupForm onAddMeetup={addMeetupHandler} />
const submitHandler = (e) => {
e.preventDefault();
const enteredTitle = titleInputRef.current.value;
const enteredImage = imageInputRef.current.value;
const enteredAddress = addressInputRef.current.value;
const enteredDescription = descriptionInputRef.current.value;
const meetupData = {
title: enteredTitle,
image: enteredImage,
address: enteredAddress,
description: enteredDescription,
}
props.onAddMeetup(meetupData);
};
const removeIngredientHandler = (ingredientId) => {
fetch(
`url.../${ingredientId}.json`,
{
method: "DELETE",
}
).then(() => {
setUserIngredients((prevIngredients) =>
prevIngredients.filter((ingredient) => ingredient.id !== ingredientId)
);
});
};
fetchAPI 인자의 엔드포인트와 .json 사이에 삭제하고자 하는 id를 전달한다.
이후 setState을 이용해 삭제된 데이터를 제외한 화면을 렌더링 해준다.