Class ComponentEffect

java.lang.Object
com.vaadin.flow.component.ComponentEffect

public final class ComponentEffect extends Object
The utility class that provides helper methods for using Signal effects in a context of a given component's life-cycle.

It ultimately creates a Signal effect, i.e. a call to Signal.effect(Runnable), that is automatically enabled when a component is attached and disabled when the component is detached. Additionally it provides methods to bind signals to component according to a given value setting function.

Since:
24.8
  • Method Details

    • effect

      public static <C extends Component> Registration effect(C owner, Runnable effectFunction)
      Creates a Signal effect that is owned by a given component. The effect is enabled when the component is attached and automatically disabled when it is detached.

      Example of usage:

       Registration effect = ComponentEffect.effect(myComponent, () -> {
           Notification.show("Component is attached and signal value is "
                   + someSignal.value());
       });
       effect.remove(); // to remove the effect when no longer needed
       
      Type Parameters:
      C - the type of the component
      Parameters:
      owner - the owner component for which the effect is applied, must not be null
      effectFunction - the effect function to be executed when any dependency is changed, must not be null
      Returns:
      a Registration that can be used to remove the effect function
      See Also:
    • bind

      public static <C extends Component, T> Registration bind(C owner, Signal<T> signal, SerializableBiConsumer<C,T> setter)
      Binds a signal's value to a given owner component in a way defined in setter function and creates a Signal effect function executing the setter whenever the signal value changes.

      Example of usage:

       Registration effect = ComponentEffect.bind(mySpan, stringSignal,
               Span::setText);
       effect.remove(); // to remove the effect when no longer needed
      
       ComponentEffect.bind(mySpan, stringSignal.map(value -> !value.isEmpty()),
               Span::setVisible);
       
      Type Parameters:
      C - the type of the component
      T - the type of the signal value
      Parameters:
      owner - the owner component for which the effect is applied, must not be null
      signal - the signal whose value is to be bound to the component, must not be null
      setter - the setter function that defines how the signal value is applied to the component, must not be null
      Returns:
      a Registration that can be used to remove the effect function
      See Also: