Skip to content

๐Ÿ“– ๋ฆฌ์•กํŠธ ํ›…์Šค

leejungeun edited this page Dec 20, 2022 · 2 revisions

React hooks

useState

State๋ž€ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‹œ๊ณ„ ์ปดํฌ๋„ŒํŠธ๋Š” time์„ ์ƒํƒœ๋กœ ๊ฐ€์ง„๋‹ค.

const [state, setState] = useState(์ดˆ๊ธฐ๊ฐ’);

setState๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ state๋ฅผ ๊ฐ„ํŽธํ•˜๊ฒŒ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. state๋ฅผ ๋ณ€๊ฒฝํ•˜๋ฉด ์žฌ๋ Œ๋”๋ง์ด ๋˜์–ด ํ™”๋ฉด์ด ์—…๋ฐ์ดํŠธ ๋œ๋‹ค.

useEffect

  1. Mount ํ™”๋ฉด์— ์ฒซ ๋ Œ๋”๋ง
  2. Update ๋‹ค์‹œ ๋ Œ๋”๋ง
  3. Unmount ํ™”๋ฉด์—์„œ ์‚ฌ๋ผ์งˆ

๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ์‹คํ–‰

useEffect(()=>{
  //์ž‘์—…...
});

ํ™”๋ฉด์— ์ฒซ ๋ Œ๋”๋ง ๋  ๋•Œ, value๊ฐ’์ด ๋ฐ”๋€” ๋•Œ ์‹คํ–‰

useEffect(()=>{
  //์ž‘์—…...
} [value]);

์˜์กด์„ฑ ๋ฐฐ์—ด์ด ๋น„์–ด ์žˆ๋‹ค๋ฉด ํ™”๋ฉด์— ์ฒซ ๋ Œ๋”๋ง ๋  ๋•Œ๋งŒ ์‹คํ–‰

clean Up - ์ •๋ฆฌ ํ™”๋ฉด์— ์ฒซ ๋ Œ๋”๋ง ๋  ๋•Œ, value๊ฐ’์ด ๋ฐ”๋€” ๋•Œ ์‹คํ–‰

useEffect(()=>{
  //๊ตฌ๋…...
  return () => {
    //๊ตฌ๋… ํ•ด์ง€...
  }
} [value]);

useRef

const ref = useRef(value)
//{current: value}

ref ์˜ค๋ธŒ์ ํŠธ๋Š” ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋ฐ˜ํ™˜๋œ ref๋Š” ์ปดํฌ๋„ŒํŠธ์˜ ์ „ ์ƒ์• ์ฃผ๊ธฐ๋ฅผ ํ†ตํ•ด ์œ ์ง€๊ฐ€ ๋œ๋‹ค. ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ณ„์†ํ•ด์„œ ๋ Œ๋”๋ง ๋˜์–ด๋„ unmount๋˜๊ธฐ ์ „ ๊นŒ์ง€๋Š” ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์œ ์ง€.

  • ์–ด๋– ํ•œ ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ์ €์žฅ๊ณต๊ฐ„, ๋ณ€๊ฒฝ ์‹œ ๋ Œ๋”๋ง์„ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ๋ง์•„์•ผ ํ•  ๊ฐ’ state์˜ ๋ณ€ํ™” -> ๋ Œ๋”๋ง -> ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€ ๋ณ€์ˆ˜๋“ค ์ดˆ๊ธฐํ™” ref์˜ ๋ณ€ํ™” -> no ๋ Œ๋”๋ง -> ๋ณ€์ˆ˜๋“ค์˜ ๊ฐ’์ด ์œ ์ง€๋จ state์˜ ๋ณ€ํ™” -> ๋ Œ๋”๋ง -> ๊ทธ๋ž˜๋„ ref์˜ ๊ฐ’์€ ์œ ์ง€๋จ

  • DOM ์š”์†Œ์— ์ ‘๊ทผ focus()

useContext

๋‹ค์–‘ํ•œ ๋ ˆ๋ฒจ์— ์žˆ๋Š” ๋งŽ์€ ์ปดํฌ๋„ŒํŠธ์— ์ „์—ญ์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.

  • Context๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์–ด๋ ค์›Œ ์งˆ ์ˆ˜์žˆ๋‹ค.
  • Prip Drilling์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•œ ๋ชฉ์ ์ด๋ผ๋ฉด ์ปดํฌ๋„ŒํŠธ ํ•ฉ์„ฑ์„ ๋จผ์ € ๊ณ ๋ คํ•˜์ž.

useMemo

Memoization: ๋™์ผํ•œ ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค๋ฉด ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ’์„ ๋งจ ์ฒ˜์Œ ๊ณ„์‚ฐํ•  ๋•Œ ์บ์‹œ์— ๋‘ฌ์„œ ์บ์‹œ์—์„œ ๊บผ๋‚ด ์‚ฌ์šฉ.

function Component() {
  const value = useMemo( () => caculate(), []
  )

  return <div>{value}</div>
}

๋ Œ๋”๋ง -> Component ํ•จ์ˆ˜ ํ˜ธ์ถœ, Memoization -> ๋ Œ๋”๋ง -> Component ํ•จ์ˆ˜ ํ˜ธ์ถœ, Memoize๋œ ๊ฐ’์„ ์žฌ์‚ฌ์šฉ

const value = useMemo( () => {
  return caculate
},[]);

๊ผญ ํ•„์š”ํ•  ๋•Œ๋งŒ

useCallback

  • ์ปดํฌ๋„ŒํŠธ ์ตœ์ ํ™”
cosnt caculate = useCallback((num) => {
  return num + 1;
}, [item])

useReducer

๋ณต์žกํ•œ state๋ฅผ ๋‹ค๋ฃฐ ๋•Œ useReducer๊ฐ€ ํŽธํ•  ์ˆ˜ ์žˆ๋‹ค.

// reducer - state๋ฅผ ์—…๋ฐ์ดํŠธ ํ•˜๋Š” ์—ญํ•  (์€ํ–‰) // dispatch - state ์—…๋ฐ์ดํŠธ๋ฅผ ์œ„ํ•œ ์š”๊ตฌ // action - ์š”๊ตฌ์˜ ๋‚ด์šฉ

cosnt ACTION_TYPES = {
  deposit: "deposit",
  widthdraw: "withdraw"
}

const reducer = (sate, action) => {
  switch (action.type) {
    case ACTION.type.depisit:
        return state + action.payload  
    case ACTION.type.withdraw:
        return state - action.payload
    default:
      return satae;
  } 
}

function App() {
  const [money, dispatch] = useReducer(reducer, 0);

  return (
    <button onClick={()=>{dispatch({type: ACTION_TYPE.deposit, payload: number});}>
)
}

React.memo

์ปดํฌ๋„ŒํŠธ์˜ ๋ Œ๋”๋ง ํšŸ์ˆ˜๋ฅผ ์ œํ•œ. ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠผ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋„ ์žฌ์‹คํ–‰๋˜๋Š”๋ฐ ์ด์ œ props๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋ Œ๋”๋ง ๋˜๋„๋ก ์ตœ์ ํ™”๋œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ œ๊ณต. prop check๋ฅผ ํ†ตํ•ด ์žฌ์‚ฌ์šฉ.

1.์ปดํฌ๋„ŒํŠธ๊ฐ€ ๊ฐ™์€ props๋กœ ์ž์ฃผ ๋ Œ๋”๋ง๋  ๋•Œ 2.์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ Œ๋”๋ง์ด ๋  ๋•Œ๋งˆ๋‹ค ๋ณต์žกํ•œ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค

** ์˜ค์ง props ๋ณ€ํ™”์—๋งŒ ์˜์กดํ•˜๋Š” ์ตœ์ ํ™” ๋ฐฉ๋ฒ•์ด๋‹ค. **

Custom Hooks

useInput

import {useInput} from "./useInput"

function displayMessage(message) {
  alert(message)
}

function App() {
  cosnt [inputValue, handleChange, handleSubmit] = useInput("", displayMessage)

  return (
    <input value={inputValue} onChange={handleChange} />
)
}
impoert {useState} from "react"

export function useInput(initialValue, submitAction) {
  const [inputValue, setInputValue] = useState(initialValue)
  cosnt handleChange = (e) => {
  setInputValue(e.target.value)
}

  const handleSubmit = () => {
    setInputValue("");
    submitAction(inputValue);
}

  return [inputValue, handleChange, handleSubmit]
}

useFetch

function App() {
  const { data: userData , fetchUrl } = useFetch(baeUrl), "users";
  const { data: postData , fetchUrl } = useFetch(baeUrl), "posts";
.

}
  useEffect(()=>{
    fetchUrl("users")   
},[])
  return (
    <div>
      <h1>useFetch</h1>
      <button onClick={()=>fetchUrl("users")}>Users</button>
      <button onClick={()=>fetchUrl("posts")}>posts</button>
      <button onClick={()=>fetchUrl("todos")}>todos</button>
    </div>
)
}
function useFetch(baseUrl, initialType) {
  const [data,setData] = useSatate(null);

  const fetchUrl = (type) => {
     fetch(baseUrl + "/" + type)
     .then(res => res.json()).then((res)=> setData(res)) 
  }

  useEffect(()=>{
    fetch(initialType)
  },[])

  return {
    date, fetchUrl,
  }
}