edifice.use_effect

edifice.use_effect#

class edifice.use_effect(setup, dependencies=None)[source]#

Bases:

Side-effect Hook inside a edifice.component() function.

Behaves like React useEffect.

The setup function will be called after render and after the underlying Qt Widgets are updated.

The cleanup function will be called by Edifice exactly once for each call to the setup function. The cleanup function is called after render and before the component is deleted.

If the dependencies change, then the old cleanup function is called and then the new setup function is called.

If the dependencies are None, then the new effect setup function will always be called.

If you want to call the setup function only once, then pass an empty tuple () as the dependencies.

If the setup function raises an Exception then the cleanup function will not be called. Exceptions raised from the setup function and cleanup function will be suppressed.

The setup function can return None if there is no cleanup function.

Example:

@component
def Effective(self, handler):

    def setup_handler():
        token = attach_event_handler(handler)
        def cleanup_handler():
            remove_event_handler(token)
        return cleanup_handler

    use_effect(setup_handler, handler)
Parameters:
  • setup (Callable[[], Callable[[], None] | None]) – An effect setup function which returns a cleanup function or None.

  • dependencies (Optional[Any]) –

    The effect setup function will be called when the dependencies are not __eq__ to the old dependencies.

    If the dependencies are None, then the effect setup function will always be called.

Return type:

None

Returns:

None