I hope someone can shed light on this. Using the react Route component gave me this idea and I can't figure it out
The React <Route> component seems to render the children, but it also returns variables such as match. Here's an example of what I'm talking about:
<Route exact path="/">
{({ match }) => (
<div>
{`${JSON.stringify(match)}`}
</div>
)}
</Route>
Notice that even though match was not declared anywhere in the component, the variable is returned by Route so that it can be accessed in the children.
If I wanted to do this in one of my own components, how would this be accomplished?
I tried this:
const Test = (props: any): any => {
return {
testVariable: "test variable",
render: (
<>
{props.children}
</>
)
};
};
And would expect to be able to use it like this:
<Test>
{({ testVariable }) => (
<p>{testVariable}</p>
)}
</Test>
But it now works as my expectation.
EDIT: This has been solved, thanks to novonimo's answer below. I learned that the technical term for this functionality is called Function as a Child Component (FaCC).
Here's a useful tutorial that I was able to find after reading novonimo's answer.