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๋Š” ์ถ”๊ฐ€ ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ์ผ๋ฐ˜์ ์ธ ์‹ค์ˆ˜์™€ ์ž ์žฌ์ ์ธ ๋ฒ„๊ทธ๋ฅผ ์‹๋ณ„ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋˜๋Š” ๊ฒฝ๊ณ ๋ฅผ ๋‚ด๋ณด๋‚ธ๋‹ค.

  1. ์˜ˆ๊ธฐ์น˜ ์•Š์€ ๋™์ž‘์„ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” componentWillMount ๋ฐ componentWillReceiveProps์™€ ๊ฐ™์€ ์•ˆ์ „ํ•˜์ง€ ์•Š์€ ์ˆ˜๋ช… ์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋ฅผ ์‹๋ณ„ํ•œ๋‹ค.

  2. ๋ถˆํ•„์š”ํ•œ ์žฌ๋ Œ๋”๋ง์„ ์œ ๋ฐœํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ ์„ค์ •๊ณผ ๊ฐ™์€ ๋ Œ๋”๋ง ๋ฐฉ๋ฒ•์˜ ์ž ์žฌ์ ์ธ ๋ถ€์ž‘์šฉ์„ ๊ฐ•์กฐ ํ‘œ์‹œํ•œ๋‹ค.

  3. 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