Definitelytyped: React.Children.map() 및 React.cloneElement() 예제 ν•„μš”

에 λ§Œλ“  2016λ…„ 10μ›” 26일  Β·  8μ½”λ©˜νŠΈ  Β·  좜처: DefinitelyTyped/DefinitelyTyped

React.Children.map() 에 쒋은 μ˜ˆκ°€ μ—†μŠ΅λ‹ˆλ‹€. μž…λ ₯ν•˜λŠ” 방법을 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€.

/*
<CheckboxGroup ...>
  <Checkbox ... />
  <Checkbox ... />
  <Checkbox ... />
</CheckboxGroup>
*/

// Child
class Checkbox extends React.Component<CheckboxProps, void> {
  // ...
}

// Parent
class CheckboxGroup extends React.Component<CheckboxGroupProps, void> {
  // ...

  render() {
    const checkboxes = React.Children.map(this.props.children, checkbox =>
      React.cloneElement(checkbox, {
        name: this.props.name,
        checked: this.props.checkedValues.includes(checkbox.props.value),
        onChange: this.handleCheckboxChange.bind(this)
      })
    );

    return (
      <div>
        {checkboxes}
      </div>
    );
  }
}

(맀우 일반적인 예, http://stackoverflow.com/a/32371612/990356 참쑰)

  • this.props.children λ₯Ό μž…λ ₯ν•˜λŠ” 방법?
  • React.cloneElement() λ₯Ό μž…λ ₯ν•˜λŠ” 방법?
  • React.Children.map() λ₯Ό μž…λ ₯ν•˜λŠ” 방법?

λŒ€λΆ€λΆ„μ˜ 경우 λ‹€μŒ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€. Type 'string' is not assignable to type 'ReactElement<any>' , #8131 μ°Έμ‘°

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

질문:

const checkboxes = React.Children.map(this.props.children, checkbox =>
  React.cloneElement(checkbox, {
    name: this.props.name,
    checked: this.props.checkedValues.includes(checkbox.props.value),
    onChange: this.handleCheckboxChange.bind(this)
  })
);

ν•΄κ²°μ±…:

const checkboxes = React.Children.map(props.children, (checkbox: React.ReactElement<CheckboxPropsInternal>) =>
  React.cloneElement(checkbox, {
    checked: props.checkedValues.includes(checkbox.props.value),
    onChange: handleCheckboxChange
  })
);

λͺ¨λ“  8 λŒ“κΈ€

ν’€ λ¦¬ν€˜μŠ€νŠΈλ₯Ό λ³΄λ‚΄μ£Όμ„Έμš”. κ²€ν† ν•˜κ² μŠ΅λ‹ˆλ‹€.

@vvakame ν•  수 μ—†μŠ΅λ‹ˆλ‹€. μž‘λ™ 방식을 μ΄ν•΄ν•˜μ§€ λͺ»ν•©λ‹ˆλ‹€.

저도 μš”. μ •μ˜ μž‘μ„±μžμ™€ λŒ€ν™”λ₯Ό λ‚˜λˆ„μ‹­μ‹œμ˜€.

this.props.children 에 ReactElementκ°€ μ•„λ‹Œ 값이 ν¬ν•¨λ˜μ–΄ μžˆμ§€ μ•Šλ‹€λŠ” 보μž₯이 μ—†κΈ° λ•Œλ¬Έμ— λͺ…μ‹œμ μœΌλ‘œ checkbox μœ ν˜• 주석을 μΆ”κ°€ν•˜κ±°λ‚˜ μœ ν˜• μ£Όμž₯ν•΄μ•Ό ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄:

React.Children.map(this.props.children, (checkbox: React.ReactElement<CheckboxProps>) => 
    React.cloneElement(checkbox) // should be ok
);

@vsiao thx 많이, μž‘λ™ν•©λ‹ˆλ‹€!

질문:

const checkboxes = React.Children.map(this.props.children, checkbox =>
  React.cloneElement(checkbox, {
    name: this.props.name,
    checked: this.props.checkedValues.includes(checkbox.props.value),
    onChange: this.handleCheckboxChange.bind(this)
  })
);

ν•΄κ²°μ±…:

const checkboxes = React.Children.map(props.children, (checkbox: React.ReactElement<CheckboxPropsInternal>) =>
  React.cloneElement(checkbox, {
    checked: props.checkedValues.includes(checkbox.props.value),
    onChange: handleCheckboxChange
  })
);

@tkrotoff 당신은 λ˜ν•œ

    React.Children.map(this.props.children, (child: number) => {
      return child + 1
    })

이 μ†”λ£¨μ…˜κ³Ό μ œμ•ˆλœ μ†”λ£¨μ…˜μ€ React.Children.map κ°€ κ°•λ ₯ν•œ ν˜•μ‹μ΄ μ•„λ‹ˆλΌλŠ” 사싀을 μ΄μš©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. Checkbox μœ ν˜• λŒ€μ‹  any λ₯Ό μ‚¬μš©ν–ˆμ„ μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. μ™œλƒν•˜λ©΄ 그것이 μžμ‹ μœ ν˜•μ΄ 될 κ²ƒμ΄λΌλŠ” 보μž₯이 μ—†κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. 이것은 λˆ„κ΅°κ°€ λ¬Έμžμ—΄μ„ μ „λ‹¬ν•˜μ—¬ ꡬ성 μš”μ†Œλ₯Ό 잘λͺ» μ‚¬μš©ν•˜λŠ” 경우 λŸ°νƒ€μž„ 였λ₯˜κ°€ λ°œμƒν•  κ°€λŠ₯성이 μžˆμŒμ„ μ˜λ―Έν•©λ‹ˆλ‹€.

이것은 μ–΄λ–€κ°€μš”:

class Slot extends React.PureComponent {
  render () {
    return this.props.children
  }
}

const isReactElement = (obj: {}): obj is React.ReactElement<{}> => {
  return obj.hasOwnProperty('type')
}

const isSlot = (obj: {}): obj is Slot => {
  return isReactElement(obj) && obj.type === Slot
}

const getSlots = (children: React.ReactNode) => React.Children.map(children, (child: React.ReactChild): JSX.Element | null => {
  if (isReactElement(child) && isSlot(child)) {
    return child
  }

  return null
})

class ComponentWithSlots extends React.PureComponent {
  render () {
    const [header, footer] = getSlots(this.props.children)

    return (
      <div>
        {header}
        <div>
          <h1>Welcome</h1>
          <p>This is my lovely component with slots</p>
        </div>
        {footer}
      </div>
    )
  }
}

class MyComponent extends React.PureComponent {
  render () {
    return (
      <ComponentWithSlots>
        <Slot>My Header!</Slot>
        <Slot>
          <div>github: @variousauthors</div>
        </Slot>
      </ComponentWithSlots>
    )
  }
}

λ‹€μŒμ„ λ Œλ”λ§ν•©λ‹ˆλ‹€.

My Header!
Welcome
This is my lovely component with slots

github: <strong i="17">@variousauthors</strong>

이 μ ‘κ·Ό 방식은 ReactChild κ°€ ReactElement 수 μžˆλ‹€λŠ” _do_ μœ ν˜• 정보λ₯Ό ν™œμš©ν•©λ‹ˆλ‹€(이λ₯Ό κ°μ§€ν•˜κΈ° μœ„ν•΄ type λ₯Ό μ‚¬μš©ν–ˆμ§€λ§Œ, μ›ν•œλ‹€λ©΄ 더 μ‘°μ‹¬ν•˜μ‹­μ‹œμ˜€). μš°λ¦¬λŠ” λ˜ν•œ Slot λŒ€ν•΄ 더 μ„€λͺ…적이고 μ •κ΅ν•˜κ²Œ λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€. UI λΌμ΄λΈŒλŸ¬λ¦¬κ°€ ν΄λž˜μŠ€μ— 자체 슬둯이 μ—°κ²°λœ νŒ¨ν„΄μ„ κ΅¬ν˜„ν•˜λŠ” 것을 λ³΄μ•˜μŠ΅λ‹ˆλ‹€. λ‹€μŒκ³Ό 같은 것:

<Dropdown>
  <Dropdown.Header>
    <FancyIcon>My Header!</FancyIcon>
  </Dropdown.Header>
  {dropdownItems}
</Dropdown>

μ΄λ ‡κ²Œ ν•˜λ©΄ μ‚¬μš©μžλŠ” 각 슬둯의 의미λ₯Ό λͺ…ν™•ν•˜κ²Œ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€. λ‚˜λ¨Έμ§€ μžμ‹(슬둯으둜 ꡬ문 λΆ„μ„λ˜μ§€ μ•ŠμŒ)은 children λΌλŠ” μ»¬λ ‰μ…˜μ— 남겨지고 κ°œλ³„ 카트λ₯Ό λ Œλ”λ§ν•˜μ—¬ μ‚¬μš©μžκ°€ children μ •μƒμ μœΌλ‘œ μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•˜κ³  μ‚¬μš©μž μ •μ˜ 슬둯.

νŽΈμ§‘ : λΆ„λͺ…νžˆμžˆλ‹€ λ°˜μž‘μš©ν•˜λŠ” 방법 React.isValidElement 당신이 λ‚΄ μ‚¬μš©μž 지정 λŒ€μ‹ μ— μ‚¬μš©ν•  μˆ˜μžˆλŠ” isReactElement .

νŽΈμ§‘: ν•  수 μžˆλŠ” λ‹€λ₯Έ 일은 ꡬ성 μš”μ†Œμ— children 속성을 μž…λ ₯ν•˜μ—¬ μ‚¬μš©μžκ°€ μ²΄ν¬λ°•μŠ€λ₯Ό μžμ‹μœΌλ‘œλ§Œ 전달할 수 μžˆλ„λ‘ ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. μ΄λ ‡κ²Œ ν•˜λ©΄ λŸ°νƒ€μž„ 였λ₯˜λ₯Ό λ°©μ§€ν•˜λŠ” 데 도움이 λ©λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰