3.React์ hook
React Hook ์ด๋
React Hooks๋ React ๋ฒ์ 16.8์ ๋์ ๋ ๊ธฐ๋ฅ์ผ๋ก, ์ด์ ์๋ class component์์๋ง ์ฌ์ฉํ ์ ์์๋ functional component์์ ๊ฐ๋ฐ์๊ฐ ์ํ ๋ฐ ๊ธฐํ React ๊ธฐ๋ฅ์ ์ฌ์ฉํ ์ ์๋๋ก ํ๋ค. Hooks๋ React ์ฝ๋๋ฅผ ์์ฑํ๋ ๋ ๊ฐ๋จํ๊ณ ๊ฐ๊ฒฐํ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๊ณ component ๊ฐ์ ์ฝ๋๋ฅผ ๋ ์ฝ๊ฒ ์ฌ์ฌ์ฉํ๊ณ ๊ณต์ ํ ์ ์๋๋ก ํ๋ค.
Hooks
useState
component๊ฐ functional component์ ์ํ๋ฅผ ๊ด๋ฆฌํ ์ ์๋๋ก ํ๋ ๊ฐ์ฅ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉ๋๋ hook์ด๋ค. ์ด๊ธฐ ์ํ ๊ฐ์ ๋ฐ์ ํ์ฌ ์ํ ๊ฐ๊ณผ ์ํ ๊ฐ์ ์ ๋ฐ์ดํธํ๋ ํจ์์ ๋ ๊ฐ์ง ์์๊ฐ ํฌํจ๋ ๋ฐฐ์ด์ ๋ฐํํ๋ค. ์ํ ๊ฐ์ด ๋ณ๊ฒฝ๋ ๋๋ง๋ค component๋ ์ ์ํ ๊ฐ์ผ๋ก ๋ค์ ๋ ๋๋ง๋๋ค.
const [count, setCount] = useState(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
</div>
);
useEffect
useEffect๋ฅผ ์ฌ์ฉํ๋ฉด component๊ฐ API์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ฑฐ๋ DOM์ ์ ๋ฐ์ดํธํ๋ ๊ฒ๊ณผ ๊ฐ์ side effect๋ฅผ functional component์์ ์ํํ ์ ์๋ค. side effect๋ฅผ ์ํํ๋ ํจ์๋ฅผ ์ฌ์ฉํ๊ณ component๋ฅผ ๋ ๋๋งํ ๋๋ง๋ค ์คํํ๋ค. ๊ฐ๋ฐ์๋ component๊ฐ ๋ง์ดํธ ํด์ ๋ ํ ๋๋ ๋ค์ ํจ๊ณผ๊ฐ ์คํ๋๊ธฐ ์ ์ ์คํํ ์ ๋ฆฌ ๊ธฐ๋ฅ์ ์ง์ ํ ์๋ ์๋ค.
useEffect(() => {
document.title = `You clicked ${count} times`;
}, [count]);
useContext
useContext๋ฅผ ์ฌ์ฉํ๋ฉด component๊ฐ ์์ component์์ ์ ๊ณตํ๋ ์ปจํ ์คํธ ๊ฐ์ ์ฌ์ฉํ ์ ์๋ค. ์ปจํ ์คํธ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๊ณ ํ์ฌ ์ปจํ ์คํธ ๊ฐ์ ๋ฐํํ๋ค. ์ปจํ ์คํธ ๊ฐ์ด ๋ณ๊ฒฝ๋๋ฉด component๊ฐ ์ ๊ฐ์ผ๋ก ๋ค์ ๋ ๋๋ง๋๋ค.
import React from 'react';
const UserContext = React.createContext();
export default UserContext;
import React from 'react';
import UserContext from './UserContext';
export default function App() {
const user = { name: 'John Doe', email: 'johndoe@example.com' };
return (
<UserContext.Provider value={user}>
<Greeting />
</UserContext.Provider>
);
}
import React, { useContext } from 'react';
import UserContext from './UserContext';
const user = useContext(UserContext);
export default function Greeting() {
const user = useContext(UserContext);
return (
<div>
Hello, {user.name}!
</div>
);
}
useRef
useRef๋ฅผ ์ฌ์ฉํ๋ฉด component๊ฐ ๋ค์ ๋ ๋๋งํ๋ ๋์ ์ง์๋๋ ๊ฐ์ ๋ํ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ์ฐธ์กฐ๋ฅผ ๋ง๋ค ์ ์๋ค. DOM ์์ ๋๋ ์ฌ๋ฌ ๋ ๋๋ง์์ ์ก์ธ์คํด์ผ ํ๋ ๋ค๋ฅธ ๊ฐ์ ํ ๋นํ ์ ์๋ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ref ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
const inputRef = useRef(null);
return (
<div>
<input ref={inputRef} />
<button onClick={() => inputRef.current.focus()}>Focus input</button>
</div>
);
useLayoutEffect
useLayoutEffect๋ useEffect()์ ์ ์ฌํ์ง๋ง ๋ชจ๋ DOM ๋ณํ์ด ์๋ฃ๋ ํ ๋ธ๋ผ์ฐ์ ๊ฐ ์ ๋ฐ์ดํธ๋ ํ๋ฉด์ ๊ทธ๋ฆฌ๊ธฐ ์ ์ ๋๊ธฐ์ ์ผ๋ก ์คํ๋๋ค. DOM ์์๋ฅผ ์ธก์ ํ๊ฑฐ๋ ์ ๋ฐ์ดํธ๋ DOM์ ์ ํํ ์ธก์ ์ด ํ์ํ ๊ธฐํ ์์ ์ ์ํํ๋ ๋ฐ ์ ์ฉํ๋ค.
useLayoutEffect(() => {
const dimensions = inputRef.current.getBoundingClientRect();
console.log(dimensions);
}, [inputRef.current]);
return (
<div>
<input ref={inputRef} />
</div>
);
React StrictMode ๋
React์ StrictMode๋ ๊ฐ๋ฐ ์ค์ React ์ ํ๋ฆฌ์ผ์ด์ ์ ์ ์ฌ์ ์ธ ๋ฌธ์ ๋ฅผ ๊ฐ์กฐ ํ์ํ๋ ๊ฐ๋ฐ์ ๋๊ตฌ๋ค. StrictMode๊ฐ ํ์ฑํ๋๋ฉด React๋ ์ถ๊ฐ ๊ฒ์ฌ๋ฅผ ์ํํ๊ณ ์ผ๋ฐ์ ์ธ ์ค์์ ์ ์ฌ์ ์ธ ๋ฒ๊ทธ๋ฅผ ์๋ณํ๋ ๋ฐ ๋์์ด ๋๋ ๊ฒฝ๊ณ ๋ฅผ ๋ด๋ณด๋ธ๋ค.
์๊ธฐ์น ์์ ๋์์ ์ ๋ฐํ ์ ์๋ componentWillMount ๋ฐ componentWillReceiveProps์ ๊ฐ์ ์์ ํ์ง ์์ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋๋ฅผ ์๋ณํ๋ค.
๋ถํ์ํ ์ฌ๋ ๋๋ง์ ์ ๋ฐํ ์ ์๋ ์ํ ์ค์ ๊ณผ ๊ฐ์ ๋ ๋๋ง ๋ฐฉ๋ฒ์ ์ ์ฌ์ ์ธ ๋ถ์์ฉ์ ๊ฐ์กฐ ํ์ํ๋ค.
component์์ ๋ฐ์ํ๋ ์ฒ๋ฆฌ๋์ง ์์ ์ค๋ฅ ๊ฐ์ง.
StrictMode๋ ํ๋ก๋์
๋ชจ๋์์ ์ ํ๋ฆฌ์ผ์ด์
์ ๋์์ ์ํฅ์ ์ฃผ์ง ์์ผ๋ฉฐ ๊ฐ๋ฐ ์ค์๋ง ์ฌ์ฉ๋๋ค. StrictMode๋ฅผ ํ์ฑํํ๋ ค๋ฉด ์ฑ ๋๋ ํน์ component๋ฅผ <React.StrictMode>
component๋ก ๋ํํ๊ธฐ๋ง ํ๋ฉด ๋๋ค.
v18์ด์
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
v18์ดํ
import React from 'react';
import { createRoot } from 'react-dom/client';
const container = document.getElementById('root');
if (!container) {
return;
}
const root = createRoot(container);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
Reference
Last updated