Merging styles onto children props
See original GitHub issue(not my actual use case, just a sample of the problem)
Given the view
render(){
<View>
{this.mapChildren(this.props.children)}
</View>
}
mapChildren(children){
return children.map((child) => {
var styles = Object.assign(child.props.style, {opacity:0}) // child.props.style contains an int (e.g 34)
return React.cloneElement(child, {styles})
})
}
I need to iterate through the children, attributing to each of them the style {opacity:0}, and merging with any existing styles, however the content of child.props.style is an int instead of an object that can be merged.
Given this, my only option is to accept styles through a prop on the parent object, and map them to the children with their keys, this is obviously not a desirable solution. Am I missing something, or is this use case not intended to be supported? For the record, my use case is a waterfall view, where i need to render the elements and then rearrange them after I know their sizes, but i imagine this applies to more use cases
Issue Analytics
- State:
- Created 8 years ago
- Comments:13 (8 by maintainers)
Top Results From Across the Web
How to pass props to {this.props.children} - Stack Overflow
Cloning children with new props. You can use React.Children to iterate over the children, and then clone each element with new props (shallow...
Read more >How to pass data to a React component's props.children
You can merge new props into the elements passed to props.children by cloning them. Both approaches have their advantages and disadvantages, so ...
Read more >Passing Contextual Classes And Styles Into Child ... - Ben Nadel
Ben Nadel demonstrates how to pass contextual classes and styles into child components in ReactJS. This requires both the calling context ...
Read more >Types of Props - Litho
Generally, Style objects are immutable: any time you combine Styles or add new properties to a Style, you get a new Style instance...
Read more >Building React Components Using Children Props ... - Soshace
The code above replaces the onClick prop of the child component with a new function that will execute both the original callback of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
As a general rule, you don’t want to mutate anything, what you want is to make a new object that has the old and new.
In your case you can do
how is @JohnyDays doing?
and @brentvatne cats are smarter than dogs :p