For the complete documentation index, see llms.txt. This page is also available as Markdown.

State Management and Data Flow

This tutorial covers how state management is implemented in the project using Redux Toolkit.

Overview

The project uses Redux Toolkit for state management, with a focus on:

  • Wallet state management

  • Alert/notification system

  • Type-safe state access

  • Service injection for async operations

Store Structure

The Redux store is organized as follows:

src/store/
├── index.ts           # Store configuration and setup
├── hooks.ts           # Custom hooks for Redux
├── wallet.ts          # Wallet-related utilities
└── slices/            # Redux slices
    ├── walletSlice.ts # Wallet state management
    └── alertSlice.ts  # Alert/notification system

Store Configuration

The main store configuration is in src/store/index.ts:

Key features:

  • Service injection for async operations

  • Combined reducers for different features

  • Type-safe store configuration

Slices

Wallet Slice (walletSlice.ts)

Manages all wallet-related state:

  • Wallet connection status

  • UTxO tracking

  • Balance calculations

  • WebSocket notifications

  • Reward account management

Example usage:

Alert Slice (alertSlice.ts)

Handles application alerts and notifications:

  • Alert display

  • Alert removal

  • Unique ID generation

  • Type-safe alert management

Example usage:

Custom Hooks

The project provides custom hooks in src/store/hooks.ts for common Redux operations:

  • useAppDispatch: Typed dispatch function

  • useAppSelector: Typed selector hook

  • Wallet-specific hooks

  • Alert management hooks

Example:

Best Practices

  1. State Access

    • Use custom hooks for type-safe state access

    • Keep selectors close to where they're used

    • Memoize complex selectors

  2. Actions

    • Use Redux Toolkit's createAsyncThunk for async operations

    • Keep actions focused and specific

    • Use proper typing for payloads

  3. Reducers

    • Keep reducers pure

    • Use Immer for immutable updates

    • Handle all possible action types

  4. Middleware

    • Use middleware for side effects

    • Keep middleware focused and specific

    • Use proper typing for middleware

Type Safety

The project maintains type safety through:

  • Properly typed store configuration

  • Type-safe actions and reducers

  • Typed selectors and hooks

  • Service injection typing

Example of type-safe action:

Testing

When testing Redux code:

  1. Test reducers in isolation

  2. Test action creators

  3. Test selectors

  4. Test async thunks with mocked services

Example test:

Next Steps

Now that you understand state management and data flow, you can proceed to:

  1. Authentication and Authorization

  2. API Integration and Services

Additional Resources

Last updated