Answer by ahmnouira for How to pass props to {this.props.children}
This is my solution with TypeScript support. For example, I want to pass handleClose as onClose props.In the parent component, we can do this: const renderChildren = () => Children.map(children,...
View ArticleHow to pass props to {this.props.children}
I'm trying to find the proper way to define some components which could be used in a generic way:<Parent><Child value="1"><Child value="2"></Parent>There is a logic going on for...
View ArticleAnswer by amit_183 for How to pass props to {this.props.children}
Is this what you required? var Parent = React.createClass({ doSomething: function(value) { } render: function() { return <div><Child doSome={this.doSomething} /></div> }})var Child =...
View ArticleAnswer by Dominic for How to pass props to {this.props.children}
Cloning children with new propsYou can use React.Children to iterate over the children, and then clone each element with new props (shallow merged) using React.cloneElement.See the code comment why I...
View ArticleAnswer by olenak for How to pass props to {this.props.children}
I needed to fix accepted answer above to make it work using that instead of this pointer. This within the scope of map function didn't have doSomething function defined.var Parent =...
View ArticleAnswer by Andres F Garcia for How to pass props to {this.props.children}
For a slightly cleaner way to do it, try:<div> {React.cloneElement(this.props.children, { loggedIn: this.state.loggedIn })}</div>Edit:To use with multiple individual children (the child...
View ArticleAnswer by 7puns for How to pass props to {this.props.children}
Try this<div>{React.cloneElement(this.props.children, {...this.props})}</div>It worked for me using react-15.1.Use {...this.props} is suggested in...
View ArticleAnswer by nitte93 for How to pass props to {this.props.children}
The slickest way to do this: {React.cloneElement(this.props.children, this.props)}
View ArticleAnswer by Lyubomir for How to pass props to {this.props.children}
Pass props to direct children.See all other answersPass shared, global data through the component tree via contextContext is designed to share data that can be considered “global” for a tree of React...
View ArticleAnswer by yeasayer for How to pass props to {this.props.children}
You no longer need {this.props.children}. Now you can wrap your child component using render in Route and pass your props as usual:<BrowserRouter><div><ul><li><Link...
View ArticleAnswer by Maksim Kostromin for How to pass props to {this.props.children}
Parent.jsx:import React from 'react';const doSomething = value => {};const Parent = props => (<div> { !props || !props.children ? <div>Loading... (required at least one...
View ArticleAnswer by and_rest for How to pass props to {this.props.children}
Cleaner way considering one or more children<div> { React.Children.map(this.props.children, child => React.cloneElement(child, {...this.props}))}</div>
View ArticleAnswer by sidonaldson for How to pass props to {this.props.children}
Further to @and_rest answer, this is how I clone the children and add a class.<div className="parent"> {React.Children.map(this.props.children, child => React.cloneElement(child,...
View ArticleAnswer by Alireza for How to pass props to {this.props.children}
You can use React.cloneElement, it's better to know how it works before you start using it in your app. It's introduced in React v0.13, read on for more information, so something along with this work...
View ArticleAnswer by poynting for How to pass props to {this.props.children}
None of the answers address the issue of having children that are NOT React components, such as text strings. A workaround could be something like this:// Render method of Parent componentrender(){ let...
View ArticleAnswer by aWebDeveloper for How to pass props to {this.props.children}
Some reason React.children was not working for me. This is what worked for me.I wanted to just add a class to the child. similar to changing a prop var newChildren = this.props.children.map((child)...
View ArticleAnswer by Shubham Khatri for How to pass props to {this.props.children}
According to the documentation of cloneElement()React.cloneElement( element, [props], [...children])Clone and return a new React element using element as the starting point. The resulting element will...
View ArticleAnswer by Alexandr Cherednichenko for How to pass props to {this.props.children}
Maybe you can also find useful this feature, though many people have considered this as an anti-pattern it still can be used if you're know what you're doing and design your solution well.Function as...
View ArticleAnswer by Nesha Zoric for How to pass props to {this.props.children}
If you have multiple children you want to pass props to, you can do it this way, using the React.Children.map:render() { let updatedChildren = React.Children.map(this.props.children, (child) => {...
View ArticleAnswer by Kenneth Truong for How to pass props to {this.props.children}
Passing Props to Nested ChildrenWith the update to React Hooks you can now use React.createContext and useContext.import * as React from 'react';// React.createContext accepts a defaultValue as the...
View Article