How to tell if a RichTextBlock field is empty?

I'm passing a RichTextBlock to to the RichText component from "prismic-reactjs". However, I want to check if the field is empty first to determine whether or not to render the component. Even when the field is empty in the Editor, the field still returns an array with a RichText object in it. How can I reliably tell if the field is empty or not?

Eventually landed on doing something like this, not sure how I feel about it though:

import { RichText } from "prismic-reactjs";

const fieldIsEmpty = RichText.asText(rich_text_field).length === 0;

Hello,

Thanks for posting us.

You need to apply a check for an array inside Richtext object is of length 1 and the field “text” of the first array element is empty.

Like in the below image, “desc” is Richtext component on Prismic, and is Empty and on querying, it returns an object “desc” with an array of length 1.

Screen Shot 2020-11-16 at 16.15.24

and in the code something like:
if((rich_text_field.length==1) && (rich_text_field[0].text==“”) )

Let me know if you have any other doubt.

Thank you,
Priyanka

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.