React: send ReactNode via context to an ancestor and render it there

79 Views Asked by At

Is trere a suggested way to render part of a children component in an ancestor (like the Portal but in component tree)?

I mean to send the node there by a Context or provide the Ref throught props or transport that by callbacks up? Is there a way to use real Portal?

I prefer functional components please.

const Children = () => <>
<Primary/>
<ToAncestor><Secondary/></ToAncestor>
<>

const Parent = () => <Children/>

const Ancestor = () => <>
<Parent/>
<SecondaryContentFromChildren/> <-- the magic
<>

Thank you ❤️

0

There are 0 best solutions below