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 μ°Έμ‘°
ν 리νμ€νΈλ₯Ό 보λ΄μ£ΌμΈμ. κ²ν νκ² μ΅λλ€.
@vvakame ν μ μμ΅λλ€. μλ λ°©μμ μ΄ν΄νμ§ λͺ»ν©λλ€.
μ λ μ. μ μ μμ±μμ λνλ₯Ό λλμμμ€.
ν @vsiao @thasner @basarat @pspeter3 @beckend
λ€μμ κΈ°λ°μΌλ‘ νλ Ping/μμ±μ λͺ©λ‘:
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
μμ±μ μ
λ ₯νμ¬ μ¬μ©μκ° μ²΄ν¬λ°μ€λ₯Ό μμμΌλ‘λ§ μ λ¬ν μ μλλ‘ νλ κ²μ
λλ€. μ΄λ κ² νλ©΄ λ°νμ μ€λ₯λ₯Ό λ°©μ§νλ λ° λμμ΄ λ©λλ€.
κ°μ₯ μ μ©ν λκΈ
μ§λ¬Έ:
ν΄κ²°μ± :