Raxol.UI.State.Hooks (Raxol v2.0.1)
View SourceUnified UI State Hooks with comprehensive functionality.
This module consolidates the best features from multiple hook implementations:
- Task-based safe execution from HooksFunctional
- Comprehensive error handling and Result types from HooksRefactored
- Complete context management from the base Hooks module
Provides React-like hooks functionality using pure functional patterns with robust error handling and timeout controls.
Features
use_state/1- Component state managementuse_effect/2- Side effects with cleanupuse_memo/2- Expensive computation memoizationuse_callback/2- Stable callback referencesuse_ref/1- Mutable referencesuse_reducer/2- Reducer-based state managementuse_context/1- Context access (implemented)use_async/2- Async operations (implemented)
Safety Features
- Task-based execution with configurable timeouts
- Comprehensive error logging and recovery
- Zero try/catch blocks (pure functional patterns)
- Automatic cleanup and context restoration
Migration Notes
This module replaces:
Raxol.UI.State.Hooks(basic implementation)Raxol.UI.State.HooksRefactored(Result-type implementation)Raxol.UI.State.HooksFunctional(Task-based implementation)
Summary
Functions
Clears the component context.
Clears all hooks for a component safely (from HooksRefactored).
Sets the component context for hook calls.
Hook for async operations.
Hook for creating stable callback references.
Hook for accessing context values.
Hook for performing side effects.
Hook for memoizing expensive computations.
Hook for managing reducer-based state.
Hook for creating mutable references.
Hook for managing component state.
Executes a function within the context of a specific component. Provides comprehensive context restoration and error handling.
Functions
Clears the component context.
Clears all hooks for a component safely (from HooksRefactored).
Sets the component context for hook calls.
Hook for async operations.
Returns {data, loading, error, refetch_fn}.
Examples
{user_data, loading, error, refetch} = use_async(fn ->
fetch_user(user_id)
end, [user_id])
Hook for creating stable callback references.
Examples
handle_click = use_callback(fn ->
set_count.(count + 1)
end, [count])
Hook for accessing context values.
Examples
theme = use_context(:theme)
user = use_context(:current_user)
Hook for performing side effects.
Examples
use_effect(fn ->
# Effect code
fn -> # Cleanup
# Cleanup code
end
end, [dependency])
Hook for memoizing expensive computations.
Examples
expensive_value = use_memo(fn ->
expensive_computation(data)
end, [data])
Hook for managing reducer-based state.
Examples
{state, dispatch} = use_reducer(reducer_fn, initial_state)
Hook for creating mutable references.
Examples
ref = use_ref(nil)
ref.current = "new value"
Hook for managing component state.
Examples
{count, set_count} = use_state(0)
{user, set_user} = use_state(%{name: "John"})
Executes a function within the context of a specific component. Provides comprehensive context restoration and error handling.