- API๋ก ๋ ์คํ ๋ ๋ชฉ๋ก์ ๋ถ๋ฌ์
<RestaurantList />์ ๋ด๋ ค์ค๋๋ค.- ๋ก๋ฉ ์ํ, ์๋ฌ ์ํ ๋ฑ์ ๊ณ ๋ คํ์ง ์์ต๋๋ค.
- ๋ ์คํ ๋ ์ถ๊ฐ ๋ชจ๋ฌ์์ ์ถ๊ฐํ๊ธฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด POST ์์ฒญ์ ๋ณด๋ ๋๋ค. ๋ชจ๋ฌ์ด ๋ซํ๊ณ , ๋ ์คํ ๋ ๋ชฉ๋ก์ ๋ค์ ๋ถ๋ฌ์ต๋๋ค.
- effect (feat. side effect)
- useEffect
- ์งํ ์๊ฐ: 2์๊ฐ ๋ด์ ์๋ฃํ๋ ๊ฒ์ ๋ชฉํ๋ก ํฉ๋๋ค.
์ฐ์ต์ฉ ์ฑ์ด๊ธฐ ๋๋ฌธ์ json-server๋ฅผ ํ์ฉํด ๊ฐ๋จํ ๊ฐ์ง REST API๋ฅผ ๊ตฌ์ถํด ์ฌ์ฉํฉ๋๋ค.
npm run server๋ฅผ ์คํํฉ๋๋ค. (ํน์npx json-server db.json๋ฅผ ์ง์ ์คํํด๋ ์๊ด์์ต๋๋ค)GET http://localhost:3000/restaurants์ผ๋กdb.json์ ์๋ ๋ ์คํ ๋ ๋ชฉ๋ก์ ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค.
// GET ์์
const response = await fetch("http://localhost:3000/restaurants");
// POST ์์
const response = await fetch("http://localhost:3000/restaurants", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(restaurant),
});Effects let you specify side effects that are caused by rendering itself, rather than by a particular event.
useEffect is a React Hook that lets you synchronize a component with an external system.
- API Reference: useSate > updater function
setfunction์ ํจ์๋ฅผ ๋๊ฒจ์ฃผ๋ฉดupdater function์ผ๋ก ๋์ํฉ๋๋ค. ํจ์๊ฐ ์๋ ๊ฐ์ ๋๊ฒจ์ค ๋์ ์ด๋ป๊ฒ ๋ค๋ฅธ์ง ์์๋ณด์ธ์.
๊ตฌํ์ ๋ค ํด๋ณธ ๋ค์ Introduction์์ ์ดํด๋ณด์๋ ์ค๊ณ ์์น๊ณผ ๊ด๋ จํด ์กฐ๊ธ ๋ ํ์ตํด๋ณด๊ณ ์ถ๋ค๋ฉด ์๋ ๋ฌธ์๋ค๋ ์ถ๊ฐ๋ก ํ์ธํด ๋ณด์ธ์.
You do need Effects to synchronize with external systems.
In React, data flows from the parent components to their children.
Purity in Components and Hooks is a key rule of React that makes your app predictable, easy to debug, and allows React to automatically optimize your code. Side effects should not run in render, as React can render components multiple times to create the best possible user experience. One important principle in React is local reasoning: the ability to understand what a component or hook does by looking at its code in isolation. Hooks should be treated like โblack boxesโ when they are called.