@blac/react
Version:
React bindings for BlaC — useBloc hook with automatic re-render optimization
52 lines (51 loc) • 1.77 kB
TypeScript
import { type ReactElement, type ReactNode } from 'react';
import type { ExtractArgs, StateContainerConstructor } from '@blac/core';
/**
* Props for {@link BlocProvider}.
*/
export interface BlocProviderProps<T extends StateContainerConstructor> {
/**
* The bloc class whose args are being provided to descendants.
*/
bloc: T;
/**
* Args that descendant `useBloc(bloc)` calls will resolve to when no
* own `args` are given.
*/
args: ExtractArgs<T>;
children: ReactNode;
}
/**
* Provides args to descendant `useBloc` calls for a specific bloc class via
* React context.
*
* Descendants calling `useBloc(Bloc)` without their own `args` resolve to the
* args supplied here. Own `args` on the `useBloc` call always win.
*
* Multiple `BlocProvider` wrappers for different bloc classes compose: each
* provider merges its entry into the inherited map, so nested providers for
* different blocs do not interfere.
*
* @example
* ```tsx
* <BlocProvider bloc={UserBloc} args={{ userId: 'alice' }}>
* <UserProfile />
* </BlocProvider>
* ```
*
* @example Per-mount private instance
* ```tsx
* const id = useId();
* <BlocProvider bloc={CartBloc} args={{ _id: id }}>
* <CartWidget />
* </BlocProvider>
* ```
*/
export declare function BlocProvider<T extends StateContainerConstructor>({ bloc, args, children, }: BlocProviderProps<T>): ReactElement;
/**
* Returns the args provided by the nearest {@link BlocProvider} for the given
* bloc class, or `undefined` when called outside a matching provider.
*
* Used by `useBloc` to inherit provider args when no own `args` are given.
*/
export declare function useProvidedArgs<T extends StateContainerConstructor>(BlocClass: T): ExtractArgs<T> | undefined;