Take a look at this simple example:
const List = function({ loading, entity }) {
return (
<Layout loading={loading}>
<span>Name: {entity.name}</span>
</Layout>
);
};
Layout
component is rendering its children
only when loading
is false
. But the problem here is that React
is resolving Layout
children immediatelly. Since entity
is null
(while loading=true
) I get error that it can't read name
of null
. Is there a simple way to avoid this error since this span
will always be rendered when entity
is not null
?
Currently I know about 3 options:
- Move this
span
tostateless function
which receivesentity
asprop
- Wrap whole
children
ofLayout
infunction
and then supportfunction children
inLayout
- Just use
{entity && <span>Name: {entity.name}</span>}
Why do I have to use one of these options and can I make React
to consider those children
as function and resolve block inside later on before the render?