Raxol.UI.State.Hooks (Raxol v2.0.1)

View Source

Unified 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

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

clear_component_context()

Clears the component context.

clear_component_hooks(component_id)

Clears all hooks for a component safely (from HooksRefactored).

set_component_context(component_id, process_pid \\ nil)

Sets the component context for hook calls.

use_async(fetch_fn, dependencies \\ [])

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])

use_callback(callback_fn, dependencies)

Hook for creating stable callback references.

Examples

handle_click = use_callback(fn ->
  set_count.(count + 1)
end, [count])

use_context(context_key)

Hook for accessing context values.

Examples

theme = use_context(:theme)
user = use_context(:current_user)

use_effect(effect_fn, dependencies \\ [])

Hook for performing side effects.

Examples

use_effect(fn ->
  # Effect code
  fn -> # Cleanup
    # Cleanup code
  end
end, [dependency])

use_memo(compute_fn, dependencies)

Hook for memoizing expensive computations.

Examples

expensive_value = use_memo(fn ->
  expensive_computation(data)
end, [data])

use_reducer(reducer_fn, initial_state)

Hook for managing reducer-based state.

Examples

{state, dispatch} = use_reducer(reducer_fn, initial_state)

use_ref(initial_value)

Hook for creating mutable references.

Examples

ref = use_ref(nil)
ref.current = "new value"

use_state(initial_value)

Hook for managing component state.

Examples

{count, set_count} = use_state(0)
{user, set_user} = use_state(%{name: "John"})

with_component_context(component_id, fun)

Executes a function within the context of a specific component. Provides comprehensive context restoration and error handling.