I am trying to understand when the function body of the Label function I have here will run.
export default function App() {
const label = <Label>Foo</Label>;
return <Button>{label}</Button>;
}
const Button = (props) => {
const shouldRenderChildren = false;
return <button>{shouldRenderChildren && props.children}</button>;
};
const Label = () => {
console.log("runing Label function");
return <span>label</span>;
};
I created a Codesandbox - https://codesandbox.io/s/why-is-label-method-not-called-zodvn4?file=/src/App.js
Notice how in the Label function there is a console.log. I virutally-render this component before the return statement in App. I was expecting this function would have been called. However trying that Codesandbox above, we see it is not called as there is no "runing Label function" seen in console.
If I change shouldRenderChildren to true in Button then we see that the "runing Label function" log to console, so we see Label is called. Here it was virtually-rendered and dom-rendered.
Does React.createElement not actually call the render method unless it gets dom-rendered?
