question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 8 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
vjeuxcommented, Apr 28, 2015

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

var style = [child.props.style, { opacity: 0 }];
React.cloneElement(child, {style})
0reactions
x5enginecommented, Feb 26, 2018

how is @JohnyDays doing?

and @brentvatne cats are smarter than dogs :p

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found