Quantcast
Channel: How to pass props to {this.props.children} - Stack Overflow
Browsing all 33 articles
Browse latest View live

Answer by Mnai for How to pass props to {this.props.children}

In my case React.cloneElement() was giving me to many problems and I'm using functional components with Typescript so I used children(props) as a way to pass my props to my children element. Again my...

View Article



Answer by pyjamas for How to pass props to {this.props.children}

Here's my version that works with single, multiple, and invalid children.const addPropsToChildren = (children, props) => { const addPropsToChild = (child, props) => { if...

View Article

Answer by MHD for How to pass props to {this.props.children}

In case anyone is wondering how to do this properly in TypeScript where there are one or multiple child nodes. I am using the uuid library to generate unique key attributes for the child elements...

View Article

Answer by nomadus for How to pass props to {this.props.children}

I did struggle to have the listed answers work but failed. Eventually, I found out that the issue is with correctly setting up the parent-child relationship. Merely nesting components inside other...

View Article

Answer by Vivek sharma for How to pass props to {this.props.children}

There are lot of ways to do this.You can pass children as props in parent.example 1 :function Parent({ChildElement}){ return <ChildElement propName={propValue} />}return <Parent...

View Article


Answer by Santhosh John for How to pass props to {this.props.children}

This answer is w.r.t. React v17.x...Use the children as a function and pass props to it as a render props pattern, as below: -<ParentComponent {...anyAdditionalProps}> { (actualPropsToPass) =>...

View Article

Answer by DonKoko for How to pass props to {this.props.children}

Got inspired by all the answers above and this is what I have done. I am passing some props like some data, and some components.import React from "react";const Parent = ({ children }) => { const {...

View Article

Answer by Kalpesh Popat for How to pass props to {this.props.children}

I came to this post while researching for a similar need, but i felt cloning solution that is so popular, to be too raw and takes my focus away from the functionality.I found an article in react...

View Article


Answer by amaster for How to pass props to {this.props.children}

When using functional components, you will often get the TypeError: Cannot add property myNewProp, object is not extensible error when trying to set new properties on props.children. There is a work...

View Article


Answer by Ben Carp for How to pass props to {this.props.children}

Method 1 - clone childrenconst Parent = (props) => { const attributeToAddOrReplace= "Some Value" const childrenWithAdjustedProps = React.Children.map(props.children, child =>...

View Article

Answer by ZEE for How to pass props to {this.props.children}

I think a render prop is the appropriate way to handle this scenarioYou let the Parent provide the necessary props used in child component, by refactoring the Parent code to look to something like...

View Article

Answer by omeralper for How to pass props to {this.props.children}

Render props is most accurate approach to this problem. Instead of passing the child component to parent component as children props, let parent render child component manually. Render is built-in...

View Article

Answer by Nick for How to pass props to {this.props.children}

The best way, which allows you to make property transfer is children like a function patternhttps://medium.com/merrickchristensen/function-as-child-components-5f3920a9ace9Code snippet:...

View Article


Answer by user10884362 for How to pass props to {this.props.children}

For any one who has a single child element this should do it.{React.isValidElement(this.props.children) ? React.cloneElement(this.props.children, { ...prop_you_want_to_pass }) : null}

View Article

Answer 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


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article
Browsing all 33 articles
Browse latest View live




Latest Images