I have 2 components. The component A is the parent component:
const A = ({children}) => {
const childrenWithProps = React.Children.map(
children,
(child) =>
React.cloneElement(child, {
test: 213
})
);
return <div>{childrenWithProps}</div>
}
Here I clone the children and send to it the test
prop.
The B component looks like this:
const B = () => {
return (
<div>
<A>
<div className="test">
<MyChildrenComponent/>
</div>
</A>
</div>
);
};
export default B;
How you can see, I want to send props from A
to the <MyChildrenComponent/>
, but, if I try to access the props in <MyChildrenComponent/>
, I get an empty object, but if I change from:
<A>
<div className="test">
<MyChildrenComponent/>
</div>
</A>
to
<A>
<MyChildrenComponent/>
</A>
...I get the props. Why if I removed the <div className="test">
the trick works but with the tag not? And how to solve the issue and to get props having this <div className="test">
wrapping my <MyChildrenComponent/>
?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…