Available Hooks
use_state
Adds state to a function component, similar to React’s useState().initial: Initial state value or a function returning it
- A tuple containing:
value: The current state valueset_value: A setter function that accepts either a new value or an updater function
- When you need to track component-specific state
- When state changes should trigger re-renders
- For managing simple values, objects, or observables
use_effect
Perform side effects in function components.setup: A function that performs the side effect. Can be sync or async.dependencies: If present, the effect only re-runs when dependencies change. If absent, the effect runs only on initial render.cleanup: Optional function to clean up after the effect
- For data fetching
- Setting up subscriptions
- Managing timers
- Manually manipulating the DOM
- Any side effects that need to run after render
use_ref
Preserve a mutable value for the lifetime of the component without causing re-renders.initial_value: Optional value or callable returning the value assigned toref.current
- A
MutableRefobject with a.currentproperty that can be read and written freely
- Storing references to controls
- Keeping track of previous values without triggering re-renders
- Storing mutable values that shouldn’t cause updates
- Holding timers, intervals, or other imperative handles
use_memo
Memoize a computed value between renders.calculate_value: A function that computes the value to be memoizeddependencies: If present, the value is only recomputed when dependencies change. If absent, computed only on initial render.
- A memoized value whose identity is stable between renders
- For expensive computations that don’t need to run on every render
- To preserve object/array identity between renders
- To optimize performance by avoiding unnecessary recalculations
use_callback
Memoize a function identity between renders.fn: A function to memoizedependencies: If present, fn is only re-memoized when dependencies change. If absent, memoized only on initial render.
- A memoized version of the function whose identity is stable between renders
- When passing callbacks to child components
- To prevent unnecessary re-renders of child components
- When function identity matters for equality checks
use_context
Access the current context value from a context provider.context: A context provider created bycreate_context
- The current context value, or the default value if no provider is found
- For sharing data across many components without prop drilling
- For global app state (theme, user, language)
- When multiple nested components need access to the same value
Helper Functions
on_mounted
Run exactly once after the component mounts.use_effect with dependencies=[].
Example:
on_unmounted
Run exactly once when the component unmounts.use_effect with dependencies=[] and the function as cleanup.
Example:
on_updated
Run after each post-mount render (or when dependencies change).Hook Rules
- Only call hooks at the top level: Don’t call hooks inside loops, conditions, or nested functions
- Only call hooks from function components: Hooks must be called from components decorated with
@ft.component - Hook order matters: Always call hooks in the same order on every render