edifice.use_effect#
- edifice.use_effect(setup, dependencies=None)[source]#
Side-effect Hook inside a
@componentfunction.Behaves like React useEffect.
- Parameters:
setup (
Callable[[],Callable[[],None] |None]) – An effect setup function which returns a cleanup function orNone.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
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
dependenciesin 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
dependenciesareNone, 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 thedependencies.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
Noneif 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)
You can use
use_effectto attachQWidgetevent handlers that are not supported by Edifice. For example, Edifice supports the resizeEvent with theon_resizeprop for all Base Elements, but if it didn’t, then you could assign a custom on_resize_handler function this way.use_effect to attach and remove a QWidget event handler#@component def MyComponent(self): ref_textinput: Reference[TextInput] = use_ref() textinput_size, textinput_size_set = use_state((0, 0)) def setup_resize_handler() -> Callable[[], None] | None: textinput = ref_textinput() if textinput is None or textinput.underlying is None: return def on_resize_handler(event: QtGui.QResizeEvent): textinput_size_set((event.size().width(), event.size().height())) # The resizeEvent method “can be reimplemented in a subclass to # receive widget resize events.” Or we can just assign to the # method because this is Python. textinput.underlying.resizeEvent = on_resize_handler def cleanup_resize_handler(): # Restore the original resizeEvent method. textinput.underlying.resizeEvent = types.MethodType( type(textinput.underlying).resizeEvent, textinput.underlying ) return cleanup_resize_handler use_effect(setup_resize_handler) with VBoxView(): TextInput(text="Type here").register_ref(ref_textinput) Label(text=f"TextInput size: {ref_textinput().size()}")