Hide block if no submenu in Twig
I generally handle this sort of thing like:
// First render my content and set it to a variable.
{% set submenu = drupal_entity('block', 'mainmenu', check_access=false)|render %}
// Next, check if the variable have content.
{% if submenu %}
  // If variable has content, render with wrapping div(s).
  &lt;div class=&quot;my-wrapping-div&quot;&gt;
    {{ submenu }}
  &lt;/div&gt;
{% endif %}

It's important to render the submenu whether or not it has any displayable content to make sure it's cache settings are bubbled up.
Note: this will only work if whatever your rendering returns no html. If you're seeing some wrapping divs, etc return, but no content, you can alter your if statement like {% if submenu|striptags|trim %} to strip html and check for only content.