score:15
Accepted answer
best way to add new props or override existing ones is to map your children props and clone each element using react.cloneelement
.
nice article to read about react children.
react.children.map(this.props.children, child =>
react.cloneelement(child, { newprop: 'value', existingprop: 'value' }));
you could also use children as a function. this way u can also add your props.
example:
// root component
const parent = () => {
const onclose = () => console.log('i am on close prop');
// pass onclose to children
return <mycomponent>{ children(onclose) }</mycomponent>
}
// mycomponent
const mycomponent = () => {
// pass onclose to children
return (
<div>
{/* children below */}
{(onclose) => (
<formcomponent
mynewprop={onclose}
/>
)}
</div>
);
}
Source: stackoverflow.com
Related Query
- How to safely inject props into React children?
- Inject children into react component passed by props in TypeScript
- React: How to inject React component into body?
- How to recursively inject props to each React component in a tree?
- React testing - How can I mock or inject data in the store to be provided as props to component
- How to pass props into jest test in React
- How to inject into React Component props?
- How React Testing Library can assert children props and Redux store updates?
- How can I pass props to children of React Router?
- How to check what additional props were passed into a react component that were not defined?
- How to pass props to children components in React
- how to pass props into component from route array in react
- React 16.8 hooks => how to properly inject an array of elements into the DOM
- How to passing props in React + TypeScript from parents to children component?
- Brand new to react and confused on how I omit an item from a function and also inject a button into my grid
- How to pass React props into a p5.js sketch
- How to use 'data-' props passed into a React class?
- React - how to pass component and props to children component and make that component use that props
- How to sort react components based on props being passed into it
- how to import css file in react when generating static html and inject imported css into html head tag?
- How can I inject a span into a string and have it printed in my react app as HTML
- How do I pass Meteor Subscription Data into React Component Props using ES6
- How to add more props into useController in React Hook Form with TypeScript?
- How to pass props to children according to type in React + Typescript
- How can i get children props value outside react map
- How to inject a dinamically created element into an existing div in React JSX?
- How can I Inject jQuery tooltip code into React via NPM or Hook?
- how to access children props in React
- How to pass children type to props of HOC react
- How to I convert my props data into an array in my React with Redux project?
More Query from same tag
- React testing Fake XMLHttpRequest
- Problem with loop fetching and returing response
- How can I add prices for objects
- React-redux rerenders when creating arrays
- why is React source code written in JavaScript instead of TypeScript, but still have types?
- Font Awesome Icons in material-ui Button
- React js converting closing tags to unreachable code when i try to add jsx or markup in middle
- React Router not routing correctly after sign in
- How can I change English numbers to Arabic numerals in modern HTML CSS?
- ReactJS: Uncaught TypeError: this.props.delete is not a function
- react-color Cannot read property 'value' of undefined
- Set the data in React Context from asynchronous API call
- Pre-filling values into a form by taking values from the URL
- How to prevent my Page render when I click on the button
- Updating Props (or State) in Inputs Mapped from Object Array with React
- create-react-app npm run build command
- electron-forge with two windows : how to render the second window? electron-react app
- Mapping data from an array
- Data not appearing in component when hooking up action creators, reducers with redux-thunk
- Render a list of checkboxes from api and sending an update using hooks
- I need to use my variable in multiple functions in my component, but can't seem to access it
- React router v6 match multiple paths without rerender
- TypeError: Cannot read properties of undefined (reading 'split'). Same Error using Slice too
- useEffect is called on html change
- why changing state does not trigger if it's related to props
- Loading Screen on next js page transition
- Reactjs listen for state changes in app component
- How to edit .env file after build completed reactjs
- Converting componentDidMount and componentWillUnmount
- React Native: Refs in ListView