--- title: 'Closures in React' author: 'Hun Im' date: 2024-09-11T13:53:35+09:00 category: ['POSTS'] tags: ['Javascript', 'React'] og_image: "/images/gamer.png" keywords: ['Javascript', 'React'] --- Closures are a fundamental concept in JavaScript. It's a concept that is often asked about in technical interviews, and I remember memorizing it while preparing for my own technical interviews. However, I didn't quite understand how closures are used in practice, and I often pondered how to apply them effectively. This time, I want to study the concept of closures in more depth. **Problem** *When passing a function as a parameter to `customHook`, letβs observe how the `state` value inside the function is output when the `useCustom` component is unmounted.* ```jsx import useCustom from './hooks/useCustom' import { useState } from 'react' export default function Home() { const [state, setState] = useState(0) useCustom(() => console.log('inner func state:', state)) return ( <div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]"> <div>{state}</div> <button onClick={() => setState(state + 1)}>+</button> </div> ) } ``` ```jsx import React, { useEffect } from 'react' const useCustom = (func: Function, state: any) => { console.log('params state:', state) useEffect(() => { return () => func() }, []) return <div>useCustom</div> } export default useCustom ``` **Code Explanation** 1. On the initial render, the `state` is set to 0. 2. The `console.log` inside `useCustom` outputs the value of `state`, which is the initial value of 0. 3. At this point, the `func` function passed to `useCustom` also retains the state of 0 due to closures. When the `useCustom` component is unmounted, this function executes, but the `state` remains at 0. 4. Since the dependency array is empty, the `useEffect` runs only once, and it does not re-execute even if the `state` changes. 5. If you want to use the latest `state` value, you need to add `state` to the dependency array of `useEffect`. Otherwise, due to closures, it will reference the previous state.
Β
--- title: '리μ‘νΈμμμ ν΄λ‘μ ' author: 'μν' date: 2024-09-11T13:53:35+09:00 category: ['POSTS'] tags: ['Javascript', 'React'] og_image: "/images/gamer.png" keywords: ['Javascript', 'React'] --- ν΄λ‘μ λ μλ°μ€ν¬λ¦½νΈμμ κΈ°λ³Έμ΄ λλ μμ£Ό μ€μν κ°λ μ΄λ€. κΈ°μ λ©΄μ μμ 1μμλ‘ μμ£Ό λ¬Όμ΄λ³΄λ κ°λ μ΄κΈ° λλ¬Έμ, λλ κΈ°μ λ©΄μ μ μ€λΉνλ©° μΈμ λ κΈ°μ΅μ΄ λλ€. νμ§λ§, μ€λ¬΄μμ ν΄λ‘μ κ° μ΄λ»κ² μ°μ΄λμ§ μ μ΄ν΄νμ§ λͺ»νκ³ , μ΄λ₯Ό μ€μ λ‘ μ΄λ»κ² μμ©ν΄μΌ ν μ§ κ³ λ―Όνλ μκΈ°κ° μμλ€. μ΄λ²μ ν΄λ‘μ κ°λ μ λ μ¬λ μκ² κ³΅λΆν΄λ³΄λ € νλ€. **λ¬Έμ ** *`customHook`μ ν¨μλ₯Ό νλΌλ―Έν°λ‘ λκ²¨μ€ λ, `useCustom` μ»΄ν¬λνΈκ° μΈλ§μ΄νΈλλ μν©μμ ν΄λΉ ν¨μκ° μ€νλ λ, ν¨μ λ΄λΆμ `state` κ°μ΄ μ΄λ»κ² μΆλ ₯λλμ§ μ΄ν΄λ³΄μ.* ```jsx import useCustom from './hooks/useCustom' import { useState } from 'react' export default function Home() { const [state, setState] = useState(0) useCustom(() => console.log('inner func state:', state)) return ( <div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]"> <div>{state}</div> <button onClick={() => setState(state + 1)}>+</button> </div> ) } ``` ```jsx import React, { useEffect } from 'react' const useCustom = (func: Function, state: any) => { console.log('params state:', state) useEffect(() => { return () => func() }, []) return <div>useCustom</div> } export default useCustom ``` **μ½λ μ€λͺ ** 1. μ΄κΈ° λ λλ§ μ `state`λ 0μΌλ‘ μ€μ λλ€. 2. `useCustom` ν λ΄λΆμ `console.log`μ μΆλ ₯λλ `state` κ°μ μ΄κΈ° κ°μΈ 0μ΄λ€. 3. μ΄λ `useCustom`μμ λκ²¨μ€ `func` ν¨μλ ν΄λ‘μ λ‘ μΈν΄ `state`κ° 0μΈ μνλ₯Ό κΈ°μ΅νκ² λλ€. μ΄ν `useCustom` μ»΄ν¬λνΈκ° μΈλ§μ΄νΈλ λ ν΄λΉ ν¨μκ° μ€νλμ§λ§, κ·Έλμ stateλ 0μΌλ‘ λ¨μ μλ€. 4. μμ‘΄μ± λ°°μ΄μ΄ λΉ λ°°μ΄μ΄κΈ° λλ¬Έμ `useEffect`λ ν λ²λ§ μ€νλλ©°, `state`κ° λ°λλλΌλ μ¬μ€νλμ§ μλλ€. 5. λ§μ½ μ΅μ `state` κ°μ μ¬μ©νκ³ μΆλ€λ©΄, `useEffect`μ μμ‘΄μ± λ°°μ΄μ `state`λ₯Ό μΆκ°ν΄ μ£Όμ΄μΌ νλ€. κ·Έλ μ§ μμΌλ©΄ ν΄λ‘μ λ‘ μΈν΄ μ΄μ μνλ₯Ό μ°Έμ‘°νκ² λλ€.