Closed silvio-e closed 3 years ago
Would the empty value of speaker.description
be undefined
, null
, {}
or []
in your example?
I would say in the case of undefined
or null
, then an error should be thrown as technically the prop value type is incorrect, and conditionally rendering the component with v-if would be the correct approach.
If either {}
or []
are being passed as the prop value, then an error should probably not be thrown, if this is the case let me know and I'll take a proper look.
Thanks for your reply!
Yes, speaker.description
could be undefined
as Sanity wouldn't deliver a default value. So that would mean two things we can do to prevent that but that would be so much repetition all over our projects:
Setting always an empty value with correct type for the prop:
*[...]{
...,
"description": coalesce(descritption, [])
}
But writing that in every query I'm not looking forward to.
Like I wrote above, checking with `v-if="speaker.description".
But also that would be in every place we use it.
If we forget one of these points a content editor could accidentally break the page when she forgets/ignores a content field.
So therefore I thought it would be handy if an undefined
could be "ignored". :)
Could this be solved with the following (untested)?
<BlockContent :blocks="speaker.description || []" />
If not, I will give some more thought to ignoring undefined, but it does seem to me that the conditional rendering approach is the 'proper' way to do things here.
Hey! :)
Would it make sense to avoid an error when retrieving an empty or undefined value for :blocks?
I have the situation that the
speaker.description
could be accidentally empty because sanity doesn't have a default or empty value for empty fields:Which would lead in nuxt.js to a server error.
I could of course check like:
But I would like to avoid doing so everywhere and everytime I use the component. Looking forward to your feedback!