New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need React.Children.map() and React.cloneElement() examples #12267
Comments
please send a pull request. I'll review it. |
@vvakame I can't, I don't understand how it is supposed to work |
me too. please make conversations with definition authors. |
you'll need to type-annotate or type-assert React.Children.map(this.props.children, (checkbox: React.ReactElement<CheckboxProps>) =>
React.cloneElement(checkbox) // should be ok
); |
@vsiao thx a lot, it works! |
Question: 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)
})
); Solution: const checkboxes = React.Children.map(props.children, (checkbox: React.ReactElement<CheckboxPropsInternal>) =>
React.cloneElement(checkbox, {
checked: props.checkedValues.includes(checkbox.props.value),
onChange: handleCheckboxChange
})
); |
@tkrotoff you could also do this:
This and the suggested solution are just taking advantage of the fact that How about this: 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>
)
}
} Which will render:
This approach takes advantage of the type information we do have, which is that a
This way the user is clear on the meaning of each slot. The rest of the children (those not parsed out as slots) are left in a collection called EDIT: React apparently has a method EDIT: Something else you can do is type the |
React.Children.map()
is missing a good example, I don't understand how to type it:(very common example, see http://stackoverflow.com/a/32371612/990356)
this.props.children
?React.cloneElement()
?React.Children.map()
?Most of the time I end up with the following error:
Type 'string' is not assignable to type 'ReactElement<any>'
, see #8131The text was updated successfully, but these errors were encountered: