avatar
Published on

useComponentWillMount??

Author
  • avatar
    Name
    yceffort
useEffect(() => {
  //.. do something
}, []) // empty deps

μ΄λ ‡κ²Œ μ˜μ‘΄μ„±μ΄ λΉ„μ–΄μžˆλŠ” useEffectκ°€ componentDidMount와 λΉ„μŠ·ν•œ 타이밍에 λ™μž‘ν•˜λŠ” 것이 μ•„λ‹ˆλΌλŠ” 사싀은 μ΄λŸ°μ €λŸ° λΈ”λ‘œκ·Έ 글에 많이 λ‚˜μ™€μžˆλ‹€. (사싀 κ°€μž₯ λΉ„μŠ·ν•œκ±΄ useLayoutEffectλ‹€.)

[] λŠ” μ΄νŽ™νŠΈμ— λ¦¬μ•‘νŠΈ 데이터 흐름에 κ΄€μ—¬ν•˜λŠ” μ–΄λ– ν•œ 값도 μ‚¬μš©ν•˜μ§€ μ•Šκ² λ‹€λŠ” λœ»μž…λ‹ˆλ‹€. κ·Έλž˜μ„œ ν•œ 번 μ μš©λ˜μ–΄λ„ μ•ˆμ „ν•˜λ‹€λŠ” λœ»μ΄κΈ°λ„ ν•©λ‹ˆλ‹€.

https://iqkui.com/ko/a-complete-guide-to-useeffect/

https://yceffort.kr/2020/10/think-about-useEffect

κ·ΈλŸ¬λ‚˜ μ—¬μ „νžˆ λ§Žμ€ μ‚¬λžŒλ“€μ΄ (λ‚˜λ₯Ό ν¬ν•¨ν•΄μ„œ) 라이프 사이클 λ©”μ†Œλ“œμ˜ ν–₯κΈ°μ—μ„œ λ²—μ–΄λ‚˜μ§€ λͺ»ν•˜κ³  μžˆλŠ” 것 κ°™λ‹€.

componentWillMount

https://ko.reactjs.org/docs/react-component.html#unsafe_componentwillmount

componentWillMountλŠ” λ§κ·ΈλŒ€λ‘œ μ»΄ν¬λ„ŒνŠΈκ°€ 마운트 되기 직전에 μ‹€ν–‰λ˜λŠ” 라이프 사이클 λ©”μ†Œλ“œλ‹€. κ·ΈλŸ¬λ‚˜ μ΄λ¦„μ—μ„œ λ³΄μ΄λŠ” 것 처럼, deprecated κ°€ λ˜μ—ˆκ³ , μ–Όλ§ˆμ „μ— λ‚˜μ˜€λŠ” v17μ—μ„œλŠ” μ™„μ „νžˆ μ‚¬λΌμ‘Œλ‹€.

https://reactjs.org/blog/2018/03/29/react-v-16-3.html#component-lifecycle-changes

For example, with the current API, it is too easy to block the initial render with non-essential logic. In part this is because there are too many ways to accomplish a given task, and it can be unclear which is best. We’ve observed that the interrupting behavior of error handling is often not taken into consideration and can result in memory leaks (something that will also impact the upcoming async rendering mode). The current class component API also complicates other efforts, like our work on prototyping a React compiler.

이유인 즉, λ Œλ”λ§μ— ν•„μš”ν•˜μ§€ μ•Šμ€ λ‘œμ§μ„ λ Œλ”λ§ 직전에 (==componentWillMount) λ„£μ–΄μ„œ λ Œλ”λ§μ„ λ°©ν•΄ν•˜λŠ” κ²½μš°κ°€ λ§Žμ•„μ‘Œλ‹€λŠ” 것이닀. 그리고 μ΄λŸ¬ν•œ λ™μž‘μ€ λ©”λͺ¨λ¦¬ μœ μΆœμ„ λ‚³λŠ” κ²½μš°κ°€ 많기 λ•Œλ¬Έμ— 지원을 μ€‘λ‹¨ν–ˆλ‹€κ³  λ°ν˜”λ‹€.

κ·ΈλŸ¬λ‚˜ μ•„μ§κΉŒμ§€λ„ λ§Žμ€ λ¦¬μ•‘νŠΈ λΌμ΄λΈŒλŸ¬λ¦¬λ“€μ΄ componentWillMount에 μ˜μ‘΄ν•˜κ³  μžˆλ‹€.

useComponentWillMount

근데 κ·ΈλŸΌμ—λ„ λΆˆκ΅¬ν•˜κ³  정말 정말 mountκ°€ 되기 직전에 무언가λ₯Ό ν•΄μ•Όν•œλ‹€λ©΄, 근데 μ“°κ³  μžˆλŠ” μ»΄ν¬λ„ŒνŠΈκ°€ ν•¨μˆ˜ν˜•μ΄λΌκ³  ν•œλ‹€λ©΄ μ–΄λ–»κ²Œ ν•΄μ•Όν• κΉŒ?

export const useComponentWillMount = (func) => {
  const willMount = useRef(true)
  if (willMount.current) func()
  willMount.current = false
}

useRefλŠ” 맀번 λ Œλ”λ§ν•  λ•Œ λ™μΌν•œ ref객체λ₯Ό μ œκ³΅ν•œλ‹€. λ”°λΌμ„œ funcκ°€ λ”±ν•œλ²ˆ μ‹€ν–‰λ˜λ„λ‘ 보μž₯ν•  수 μžˆλ‹€. κ·Έλ ‡λ‹€λ©΄ 이것이 mount되기 직전에 μ‹€ν–‰λœλ‹€κ³  λ³Ό 수 μžˆλŠ” μ΄μœ λŠ” λ¬΄μ—‡μΌκΉŒ?

useEffectλŠ” λ§κ·ΈλŒ€λ‘œ λΆ€μˆ˜νš¨κ³Όλ₯Ό λ°œμƒμ‹œν‚€λŠ” hook이기 λ•Œλ¬Έμ— λ Œλ”λ§μ΄ 된 이후에 μ‹€ν–‰λœλ‹€.

κ·ΈλŸ¬λ‚˜ useRef μ½”λ“œμ—μ„œ ν•¨μˆ˜κ°€ λ„˜μ–΄μ˜€κ²Œ 되면, (ν•¨μˆ˜κ°€ ν˜ΈμΆœλ˜λŠ” μ‹œμ μ—) λ”± ν•œλ²ˆ λ°”λ‘œ μ‹€ν–‰ν•  수 있게 λœλ‹€. 그리고 이 값은 λ˜ν•œ μ»΄ν¬λ„ŒνŠΈμ˜ 전체 라이프 사이클 λ‚΄μ—μ„œ κ³„μ†ν•΄μ„œ μœ μ§€λ˜λŠ” 것을 λ¦¬μ•‘νŠΈμ—μ„œ 보μž₯ν•΄μ€€λ‹€.

이 κΈ°λŠ₯은 ν΄λž˜μŠ€μ—μ„œ μΈμŠ€ν„΄μŠ€ ν•„λ“œλ₯Ό μ‚¬μš©ν•˜λŠ” 방법과 μœ μ‚¬ν•œ μ–΄λ–€ 가변값을 μœ μ§€ν•˜λŠ” 데에 νŽΈλ¦¬ν•©λ‹ˆλ‹€.

useRefλŠ” 맀번 λ Œλ”λ§μ„ ν•  λ•Œ λ™μΌν•œ ref 객체λ₯Ό μ œκ³΅ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€.

또 μž¬λ°ŒλŠ” 방법은 μ΄κ²ƒμ΄μ—ˆλ‹€.

export const useComponentWillMount = (func) => {
  useMemo(func, [])
}

μ˜μ‘΄μ„±μ΄ μ—†λŠ” useMemoλ₯Ό μ“°κ²Œ 되면, ν•¨μˆ˜κ°€ λ‹€μ‹œ 호좜 될 일이 μ—†μœΌλ―€λ‘œ λ Œλ”λ§ 직전에 μ‹€ν–‰λ˜λŠ” 것을 보μž₯ν•  수 μžˆλ‹€.

μ°Έκ³ : https://stackoverflow.com/questions/53464595/how-to-use-componentwillmount-in-react-hooks