Skip to content

@blac/react

React integration hooks and components for BlaC state management.

Quick Reference

Hooks: configureBlacReact, useBloc, useBlocActions

Interfaces: BlacReactConfig, UseBlocActionsOptions, UseBlocOptions

Types: UseBlocReturn

Hooks

configureBlacReact

Configure global defaults for @blac/react hooks.

typescript
export declare function configureBlacReact(config: Partial<BlacReactConfig>): void;
ParameterTypeDescription
configPartial<BlacReactConfig>Partial configuration to merge with defaults

Examples:

ts
import { configureBlacReact } from '@blac/react';

// Disable auto-tracking globally
configureBlacReact({
  autoTrack: false
});

useBloc

React hook that connects a component to a state container with automatic re-render on state changes.

Supports three tracking modes: - Auto-tracking (default): Automatically detects accessed state properties via Proxy - Manual dependencies: Explicit dependency array like useEffect - No tracking: Returns full state without optimization

Note: This hook does NOT support stateless containers (StatelessCubit, StatelessVertex). Use useBlocActions() instead for stateless containers.

typescript
export declare function useBloc<T extends StateContainerConstructor = StateContainerConstructor>(BlocClass: StatefulContainer<T>, options?: UseBlocOptions<T>): UseBlocReturn<T, ExtractState<T>>;
ParameterTypeDescription
BlocClassStatefulContainer<T>The state container class to connect to (must not be stateless)
optionsUseBlocOptions<T>Configuration options for tracking mode and instance management

Returns: Tuple with [state, bloc instance, ref]

Examples:

Basic usage

ts
const [state, myBloc, ref] = useBloc(MyBloc);

With manual dependencies

ts
const [state, myBloc] = useBloc(MyBloc, {
  dependencies: (state) => [state.count]
});

With isolated instance

ts
const [state, myBloc] = useBloc(MyBloc, {
  instanceId: 'unique-id'
});

useBlocActions

React hook that connects to a state container instance without triggering re-renders. Use this when you only need to call actions on the bloc without subscribing to state changes.

This is the recommended hook for stateless containers (StatelessCubit, StatelessVertex). It also works with regular Cubit/Vertex when you don't need state subscriptions.

typescript
export declare function useBlocActions<T extends StateContainerConstructor = StateContainerConstructor>(BlocClass: T, options?: UseBlocActionsOptions<InstanceType<T>>): InstanceType<T>;
ParameterTypeDescription
BlocClassTThe state container class to connect to (supports both stateful and stateless)
optionsUseBlocActionsOptions<InstanceType<T>>Configuration options for instance management and lifecycle

Returns: The state container instance for calling actions

Examples:

Basic usage with stateless container

ts
class AnalyticsService extends StatelessCubit {
  trackEvent(name: string) { ... }
}

const analytics = useBlocActions(AnalyticsService);
analytics.trackEvent('page_view');

With stateful container (no re-renders)

ts
const myBloc = useBlocActions(MyBloc);
myBloc.someMethod(); // Won't cause re-renders

With isolated instance

ts
const myBloc = useBlocActions(MyBloc, {
  instanceId: 'unique-id'
});

Interfaces

BlacReactConfig

Global configuration for @blac/react

typescript
export interface BlacReactConfig
PropertyTypeDescription
autoTrackbooleanEnable automatic property tracking via Proxy (default: true)

UseBlocActionsOptions

Configuration options for useBlocActions hook @template TBloc - The state container type @template TProps - Props type passed to the container

typescript
export interface UseBlocActionsOptions<TBloc, TProps = any>
PropertyTypeDescription
instanceId (optional)string | numberCustom instance identifier for shared or isolated instances
onMount (optional)(bloc: TBloc) => voidCallback invoked when bloc instance mounts
onUnmount (optional)(bloc: TBloc) => voidCallback invoked when bloc instance unmounts
props (optional)TPropsProps passed to bloc constructor or updateProps

UseBlocOptions

Configuration options for useBloc hook @template TBloc - The state container type @template TProps - Props type passed to the container

typescript
export interface UseBlocOptions<TBloc extends StateContainerConstructor, TProps = ExtractProps<TBloc>>
PropertyTypeDescription
autoTrack (optional)booleanEnable automatic property tracking via Proxy (default: true)
dependencies (optional)(state: ExtractState<TBloc>, bloc: InstanceReadonlyState<TBloc>) => unknown[]Manual dependency array like useEffect (disables autoTrack)
disableGetterCache (optional)booleanDisable caching for getter tracking
instanceId (optional)string | numberCustom instance identifier for shared or isolated instances
onMount (optional)(bloc: InstanceType<TBloc>) => voidCallback invoked when bloc instance mounts
onUnmount (optional)(bloc: InstanceType<TBloc>) => voidCallback invoked when bloc instance unmounts
props (optional)TPropsProps passed to bloc constructor or updateProps

Types

UseBlocReturn

Tuple return type from useBloc hook containing state, bloc instance, and ref - [0] Current state value (with optional state type override) - [1] State container instance (bloc) for calling actions - [2] Ref object for accessing component ref (advanced use cases)

typescript
export type UseBlocReturn<TBloc extends StateContainerConstructor, S = ExtractState<TBloc>> = [S, InstanceReadonlyState<TBloc>, RefObject<ComponentRef>];

Released under the MIT License.