Rules of Hooks
Before diving in, understand these critical rules:- Only call hooks at the top level - Don’t call hooks inside loops, conditions, or nested functions
- Only call hooks in components - Hooks must be called inside functions decorated with
@ft.component - Call hooks in the same order - Hook calls must happen in the same order on every render
flet/components/component.py:258
use_state
Adds state to a component. Returns a tuple of(value, setter).
Location: flet/components/hooks/use_state.py:47
Signature
Basic Usage
Lazy Initialization
Pass a function to compute the initial state only once:Functional Updates
The setter accepts an updater function that receives the previous state:Observable State
State values can beObservable objects for shared state:
flet/components/hooks/use_state.py:71
use_effect
Perform side effects in components. Effects run after render and can optionally return a cleanup function. Location:flet/components/hooks/use_effect.py:41
Signature
Run on Every Render
Run Once on Mount
Run When Dependencies Change
Cleanup Functions
cleanup parameter:
flet/components/hooks/use_effect.py:11
Lifecycle Helpers
Flet provides convenience functions for common lifecycle patterns:on_mounted
Runs exactly once after the component mounts: Location:flet/components/hooks/use_effect.py:77
on_unmounted
Runs exactly once when the component unmounts: Location:flet/components/hooks/use_effect.py:88
on_updated
Runs after each post-mount render: Location:flet/components/hooks/use_effect.py:100
use_ref
Persist a mutable value across renders without triggering updates. Location:flet/components/hooks/use_ref.py:37
Signature
Basic Usage
Storing Mutable Values
Unlike state, updating.current doesn’t trigger a re-render:
Storing Previous Values
flet/components/hooks/use_ref.py:15
use_memo
Memoize expensive computations to avoid recalculating on every render. Location:flet/components/hooks/use_memo.py:24
Signature
Basic Usage
Expensive Calculations
Always Recompute
flet/components/hooks/use_memo.py:11
use_callback
Memoize function identity to prevent unnecessary re-renders of child components. Location:flet/components/hooks/use_callback.py:12
Signature
Basic Usage
With Multiple Dependencies
use_context
Access shared context values without prop drilling. Location:flet/components/hooks/use_context.py:88
Creating Context
Location:flet/components/hooks/use_context.py:54
Providing Context
Consuming Context
Observable Context Values
Context values can beObservable for automatic updates:
Location: flet/components/hooks/use_context.py:110
Custom Hooks
Create your own hooks by combining built-in ones:Hook Implementation Details
All hooks inherit from the baseHook class:
Location: flet/components/hooks/hook.py:11
Hooks are stored positionally in the component’s _state.hooks list. The use_hook() method manages hook creation and retrieval:
Location: flet/components/component.py:258
Each hook type has its own state container:
StateHook- Stores value, version, and observable subscriptionEffectHook- Stores setup, cleanup, dependencies, and async tasksRefHook- Stores the mutable ref objectMemoHook- Stores memoized value and previous dependenciesContextHook- Marker for context subscription ordering
Best Practices
- Follow the rules - Never call hooks conditionally or in loops
- Use dependency arrays carefully - Missing dependencies cause stale values
- Prefer functional updates - Use
set_state(lambda prev: prev + 1)for latest values - Clean up effects - Return cleanup functions to prevent memory leaks
- Extract custom hooks - Reuse stateful logic across components
- Use refs sparingly - Prefer state for values that affect rendering
- Memoize expensive operations - Use
use_memoanduse_callbackto optimize performance
Next Steps
- Build reusable Components
- Learn about Authentication with hooks
- Explore Testing hook-based components