[TIL] HOC

Dev_minΒ·2019λ…„ 11μ›” 17일
0

TIL

λͺ©λ‘ 보기
34/61

πŸ‘‰ Higher - order Component (HOC)

: ν•˜λ‚˜μ˜ ν•¨μˆ˜μΈλ°, ν•¨μˆ˜λ₯Ό ν†΅ν•˜μ—¬ μ»΄ν¬λ„ŒνŠΈμ— μš°λ¦¬κ°€ μ€€λΉ„ν•œ νŠΉμ • κΈ°λŠ₯을 λΆ€μ—¬ν•œλ‹€.

λ°˜λ³΅λ˜λŠ” μ½”λ“œλ₯Ό μ—†μ• κΈ° μœ„ν•΄μ„œ ν•˜λ‚˜μ˜ ν•¨μˆ˜λ₯Ό μž‘μ„±ν•œλ‹€. HOC의 이름을 λ§Œλ“€λ• with__ ν˜•μ‹μœΌλ‘œ 이름을 μ§“λŠ”λ‹€. HOC의 μ›λ¦¬λŠ”, νŒŒλΌλ―Έν„°λ‘œ μ»΄ν¬λ„ŒνŠΈλ₯Ό λ°›μ•„μ˜€κ³ , ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ μƒˆ μ»΄ν¬λ„ŒνŠΈλ₯Ό λ§Œλ“  λ‹€μŒμ— ν•΄λ‹Ή μ»΄ν¬λ„ŒνŠΈ μ•ˆμ—μ„œ νŒŒλΌλ―Έν„°λ‘œ λ°›μ•„μ˜¨ μ»΄ν¬λ„ŒνŠΈλ₯Ό λ Œλ”λ§ν•˜λŠ” 것이닀. 그리고, μžμ‹ μ΄ λ°›μ•„μ˜¨ props듀은 κ·ΈλŒ€λ‘œ νŒŒλΌλ―Έν„°λ‘œ λ°›μ•„ 온 μ»΄ν¬λ„ŒνŠΈμ—κ²Œ λ‹€μ‹œ μ£Όμž…ν•΄μ£Όκ³ , ν•„μš”μ— 따라 μΆ”κ°€ props도 λ„£μ–΄μ€€λ‹€.

2020.05.28 μˆ˜μ • πŸ‘‡

// HOC ν˜•νƒœ
import React, { Component } from 'react'

const withHoc = (parameter) => (WrappedComponent) => {
  return class extends Component {
    render(){
      return (
        <WrappedComponent {...this.props} />
      )
   }
}

export default withHoc

import React, { Component } from 'react'
import withHoc from './withHoc';

class TestComponent extends Component {
  {/*...*/}	
}

export default withHoc(parameter)(TestComponent)
profile
TIL record

0개의 λŒ“κΈ€