How to dynamically add a prop to a React child element
I am passing children to my React component and would like to dynamically add props to the children from the parent component.
Here is an example code:
<ParentComponent>
<ChildComponent prop1="test" />
<ChildComponent prop1="test2" />
</ParentComponent>
My ParentComponent
looks like this:
import React from 'react';
const ParentComponent = ({ children }: React.PropsWithChildren<unknown>) => {
return (
<>
{children} // here I would like to dynamically add props to those children
</>
);
};
3914 views