You can use React.Children
and React.cloneElement
api in your MyCustomRectComponentA
component.
eg:
import React, { Component, Children , cloneElement } from 'react'
class MyCustomRectComponentA extends Component {
state = {
offset: '50px'
}
render() {
return (
<div>
// mapping through every children elements
// passed to "MyCustomRectComponentA" component
{Children.map(this.props.children, child => {
// you have to clone the child element
// because they are read-only
// and pass the "state" as props
return cloneElement(child, {
offsetValue: this.state.offset
})
})}
</div>
)
}
}
export default MyCustomRectComponentA
After this you can update your parent component containing MyCustomRectComponentA
and AnotherCustomComponentB
to
public render() {
return (
<MyCustomRectComponentA>
<AnotherCustomComponentB />
</MyCustomRectComponentA>
);
}
What we have done is that we passed <AnotherCustomComponentB />
component as children to </MyCustomRectComponentA>
component.
And in </MyCustomRectComponentA>
component
1) we mapped through children component passed to it
2) cloned the children component
3) passed state "offset" as props to children component
For more detail about React.Children
you can go to this source