Overview
Functional component wrapper used by the component renderer. A component keeps hook state, tracks observable argument dependencies, renders a function body into controls, and schedules updates/effects through the active page session. Source:flet.components.component:60
Components enable a reactive, functional programming model in Flet similar to React hooks. They allow you to build reusable UI pieces with encapsulated state and lifecycle.
Class Signature
Creating Components
Use the@component decorator to create functional components:
Internal Structure
Component State
The component function being wrapped.Source:
component.py:70Metadata: skip=TruePositional arguments passed to the component function.Source:
component.py:71Metadata: skip=TrueKeyword arguments passed to the component function.Source:
component.py:72Metadata: skip=TrueWhether this component is memoized (only re-renders when props change).Source:
component.py:80Metadata: skip=TrueInternal mutable state backing the component instance.Source:
component.py:76Stores:- Hook registry and cursor
- Memoization snapshots
- Mount flags
- Observable subscriptions
skip=TrueThe rendered body (output) of the component.Source:
component.py:83Methods
Lifecycle Methods
Render component body and patch changed output to the session.Source:
component.py:98This method:- Resets hook cursor
- Detaches old observable subscriptions
- Subscribes to new observables
- Renders the component function
- Freezes the output
- Memoizes if needed
- Patches changes to session
- Runs render effects
Called before the component is updated.Source:
component.py:144Checks memoization and re-renders if:- Component is dirty (state changed)
- Not memoized
- Props (args/kwargs) have changed
Mark component mounted and run mount-time effects.Source:
component.py:329Called after component is added to the page tree. Triggers all effect hooks for initial execution.Mark component unmounted, detach observable listeners, and run cleanups.Source:
component.py:338Called before component is removed from the page tree. Executes cleanup functions from all effect hooks.Hook Management
Return hook instance for current render slot, creating it if missing.Source:
component.py:258Parameters:default(Callable): Factory function to initialize a new hook
use_* hooks like use_state, use_effect, etc.Internal Update Scheduling
Mark component dirty and enqueue a session update.Source:
component.py:181Called when component state changes (e.g., from set_state in use_state).Enqueue effect or effect-cleanup execution in session scheduler.Source:
component.py:190Parameters:hook(EffectHook): Effect hook to executeis_cleanup(bool): Whether to run hook cleanup instead of effect body
Observable Subscriptions
Attach subscriptions for observable positional/keyword arguments.Source:
component.py:202Automatically subscribes to any Observable objects passed as props.Subscribe component updates to an observable argument.Source:
component.py:218Parameters:observable(Observable): Observable object to subscribe to
Dispose and clear all observable subscriptions for this component.Source:
component.py:249Component State (_ComponentState)
Internal state structure for components:Renderer
TheRenderer class coordinates functional component rendering and context stacks.
Source: component.py:362
Renderer Methods
Render a root callable within this renderer context/frame.Source:
component.py:427Parameters:root_fn: Callable producing component output*args: Positional arguments forwarded to root_fn**kwargs: Keyword arguments forwarded to root_fn
render_component
(fn: Callable[..., Any], args: tuple[Any, ...], kwargs: dict[str, Any], key=None) -> Component
Create a frozen Component wrapper for a function call.Source:
component.py:444Parameters:fn: Component function decorated with @componentargs: Positional arguments for the component functionkwargs: Keyword arguments for the component functionkey: Optional identity key
ValueError if fn is not marked as a componentMark next rendered component as memoized.Source:
component.py:378Push a context value for the given key.Source:
component.py:385Parameters:key: Context identity tokenvalue: Context value to make active
Pop current context value for key and remove empty stacks.Source:
component.py:397Parameters:key: Context identity token
Usage Examples
Basic Component
Stateful Component with Hooks
Memoized Component
Component with Effects
Component with Observable Props
Component Lifecycle
- Creation: Component function is called with props (args/kwargs)
- Initialization: Hooks are initialized on first render
- Mounting:
did_mount()is called, mount effects run - Rendering: Component function executes, returns controls
- Updating: When state/props change, component re-renders
- Unmounting:
will_unmount()is called, cleanup functions run
Best Practices
- Hook Order: Always call hooks in the same order on every render
- Memoization: Use
@memofor expensive components that don’t need frequent updates - Effects: Use empty dependency array
[]for mount-only effects - Cleanup: Return cleanup functions from effects to prevent memory leaks
- Props: Keep props immutable; use observables for reactive data
See Also
- Hooks - Available hooks for components
- Observable - Reactive data containers
- @component decorator - Component decorator
- @memo decorator - Memoization decorator
- Page - Top-level page container