- Published on
useComponentWillMount??
- Author
- 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