You can use this pattern to check for empty variables. If the field has a value then your <p>
will render. If haaksort
doesn’t exist on item.fields
or the value of haaksort
is undefined then the <p>
won’t render.
{item.fields.haaksoort && (<p>haaksoort: {item.fields.haaksoort}</p>)}
A couple of notes on this though:
item
andfields
must also exist.- This will not yield the desired result if
haaksoort
is the empty string
If you need to do checking on the above conditions then a middleware function like mentioned in the question comment will best serve you. Then you can do something like this in your render code:
{isEmpty(item.fields.haaksoort) ? '' : (<p>haaksoort: {item.fields.haaksoort}</p>)
Note that this is a simplistic example of a call to a middleware function that assumes that item
exists with a property fields
that also exists.
3
solved React json check if empty?