edifice.use_effect#

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

Side-effect Hook inside a @component function.

Behaves like React useEffect.

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

The setup function may return a cleanup function. If the dependencies in the next render are not __eq__ to the dependencies from the last render, then the cleanup function is called and then the new setup function is called.

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 are None, then the new effect setup function will always be called after every render.

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.

The setup function and cleanup function can call the setter of a use_state() Hook to update the application state.

use_effect to attach and remove an event handler#
@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