@channel-state/react
Version:
React hooks for channel-state, providing seamless integration with React applications for cross-context state management.
47 lines (44 loc) • 1.75 kB
TypeScript
import * as _channel_state_core from '@channel-state/core';
import { ChannelStore } from '@channel-state/core';
/**
* A React hook that provides access to a ChannelStore's state and a setter function.
* It uses `useSyncExternalStore` for efficient and concurrent-safe updates.
* @template T The type of the state managed by the ChannelStore.
* @param store The ChannelStore instance to connect to.
* @returns A tuple containing the current state and a function to update the state.
* @example
* ```tsx
* import { useChannelState } from '@channel-state/react';
* import { ChannelStore } from '@channel-state/core';
*
* const countStore = new ChannelStore<number>({ name: 'count', initial: 0 });
*
* function Counter() {
* const [count, setCount] = useChannelState(countStore);
* return (
* <button onClick={() => setCount(count + 1)}>Count: {count}</button>
* );
* }
* ```
*/
declare function useChannelState<T>(store: ChannelStore<T>): readonly [T, (newValue: T) => void];
/**
* A React hook that provides access to a ChannelStore's status.
* It uses `useSyncExternalStore` for efficient and concurrent-safe updates.
* @param store The ChannelStore instance to connect to.
* @returns The current status of the store.
* @example
* ```tsx
* import { useChannelStatus } from '@channel-state/react';
* import { ChannelStore } from '@channel-state/core';
*
* const countStore = new ChannelStore<number>({ name: 'count', initial: 0 });
*
* function StatusDisplay() {
* const status = useChannelStatus(countStore);
* return <p>Status: {status}</p>;
* }
* ```
*/
declare function useChannelStatus(store: ChannelStore<unknown>): _channel_state_core.StoreStatus;
export { useChannelState, useChannelStatus };