Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Re: Conditionally display elements

Conditionally display elements

timd
Shopify Partner
124 6 79

I'm wanting to conditionally display elements in a Checkout UI Extension if settings have been added, but if has_title or has_content_1 fails, none of the elements display.

Is this the correct way to include conditionally logic or is there another way to do it?

 

function App() {
  const { title, heading_1, content_1 } = useSettings();

  const has_title = title.trim() !== '';
  const has_content_1 = heading_1.trim() !== '' && content_1.trim() !== '';
  return (
    <>
      {has_title && (
        <Text emphasis="bold">{title}</Text>
      )}

      {has_content_1 && (
        <View border="none" padding="base">
          <Text emphasis="bold">{heading_1}</Text>
          <TextBlock>
            {content_1}
          </TextBlock>
        </View>
      )}
    </>
  );
}

 

Replies 3 (3)

Liam
Community Manager
3108 344 892

Hi Timd,

 

I've asked the checkouts team about this - will update here when I learn more. 

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

Liam
Community Manager
3108 344 892
Hi again Timd,
 
Just to clarify - you want to achieve wants the following: 
  • if either has_title or has_content_1 is false, no elements is displayed
  • meaning if both has_title and has_content_1 is true, elements are displayed

Is that correct?

Liam | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

timd
Shopify Partner
124 6 79

Hi @Liam no it should be the opposite, if has_title is false, but has_content_1 is true only the content in has_content_1 should be shown.

At the moment, if one of the conditions returns false, none of the elements are displayed.