Hello, I am trying to understand why we need another check inside the render function. My code is below. You can see that I have the check to see if foo.bar is truthy as foo?.bar && from the beginning. However, why do I need another if check if(foo.bar) {...} in order to use foo.bar variable?
Another approach that seems to be working is to check foo and foo.bar individually such as: foo && foo.bar &&
Can someone explain to me with an example? Thank you very much 🙏
{ foo?.bar &&
<div className='field'>
<FormGroupField
name={barLabel ''}
id={"id-" + barLabel}
label={barLabel}
render={() => {
// If I place the return here without if(foo.bar), it errors out:
//return (<div>{utcToLocal(foo.bar).toLocaleString([], nativeJSDateFormat())}</div>)
// I have to do this:
if (foo.bar) {
return (<div>{utcToLocal(foo.bar).toLocaleString([], nativeJSDateFormat())}</div>)
}
}}
/>
</div>
}
//Other solution is to replace foo?.bar && => foo && foo.bar && as in the next line:
{ foo && foo.bar &&
<div className='field'>
<FormGroupField
name={barLabel ''}
id={"id-" + barLabel}
label={barLabel}
render={() => {
// Then this is OK
return (<div>{utcToLocal(foo.bar).toLocaleString([], nativeJSDateFormat())}</div>)
}}
/>
</div>
}