<ComponentA>
is supposed to render<ComponentB>
(not import it but rather render it as a child)<ComponentB>
comes with its own props (propsForComponentB
)- But I want
<ComponentA>
to pass one extra prop of its own to<ComponentB>
I'm thinking of something like the below, but being new to React I don't know if/how it's possible:
<ComponentA>
<ComponentB {...propsForComponentB} />
</ComponentA>
//component-a.jsx
export default function(props) {
function doSomething(dataFromChildComponent){
...
}
return(
{
/*
Here I want to render the children components while passing an extra prop
to each of them. My syntax is obviously wrong. This is mostly pseudocode to get
my point across. Is this possible in some way ?
*/
props.children.map((child) => {child componentAmethod={doSomething} })
}
)
}
//component-b.jsx
export default function(props) {
/*
here `props` should contain all of the original
`propsForComponentB` passed to ComponentB initially,
plus the extra prop `componentAmethod`
*/
function onSomething(){
props.componentAmethod(...arguments)
}
.....
...
}
To give you an example, with Ember.js you can do cool stuff like the below, out of the box:
<ComponentA as |doSomething|>
<ComponentB onSomething={doSomething}/>
</ComponentA>
doSomething
is an internal method of <ComponentA>
that is made available to whatever children are rendered inside <ComponentA>