Skip to content

Support conditional #131

Closed

Description

@MikeKry

Provide a way to set conditional [SkipOutput] attribute based on changes.

Usecase:

  • Element has on:change event that uses dispatch global
  • Multiple components, including the one sendind dispatch, are subscribed to an event
  • When event happens, some components need to be updated.
  • Because the caller also receives the event, it also gets re-rendered. And it changes the Element that has on:change event, which again causes dispatch.
  • So I need to break the cycle by using SkipOutput when receiving component is not updated.

There is currently a workaround that disposes produced html, but it is not ideal. See below.

[SkipOutput]
public async Task UpdateEnum(int selectedValue)
{
    if (BaseEnum.SelectedValue != selectedValue)
    {
       // dispatch global event, but this component is also subscribed to it.
        DispatchGlobal(new DependentEnumChangedEvent(BaseEnumName, selectedValue));
    }
}

public async Task Handle(DependentEnumChangedEvent data)
{
    if (ParentEnumName == data.EnumName)
    {
       // some logic that needs re-render
    }
    else
    {
      // also the current component is registered to DispatchGlobal, but I do not want it to re-render as it causes on:change event to fire again and makes infinite loop of dispatches
        _httpContextAccessor.HttpContext!.Response.Headers.TryAdd("Hydro-Skip-Output", "True");
    }
}

cshtml

<select id="@Model.BaseEnumName" name="@Model.BaseEnumName" on:change="@(() => Model.UpdateEnum(Param.JS<int>(getValueScript)))">
    <option value="">@T["Please select a value"]</option>
    @foreach(var value in items){
        <option value="@value.Value" selected="@value.Selected">@value.Text</option>
    }
</select>

Maybe there is some error in my understanding of lifecycle, so feel free to tell me

Activity

  1. kjeske commented on Jun 9, 2025

    @kjeske
    Contributor

    Good point, it should be possible to skip the output by calling some method inside your action/handler. Something like:

    public async Task Handle(DependentEnumChangedEvent data)
    {
        if (ParentEnumName == data.EnumName)
        {
           // some logic that needs re-render
        }
        else
        {
          this.SkipOutput();
        }
    }

    Shouldn't be hard to implement. Will definitely add it soon.

    Thanks for a good idea!

  2. self-assigned this
    on Jun 9, 2025
  3. added theissue type on Jun 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions