• A React hook that creates a synchronized state object.

    Type Parameters

    • T extends object

      The type of the state object.

    Parameters

    • initialState: T

      The initial state.

    • Optional updateEvent: string = 'set'

      The event name that triggers a state update.

    Returns ReactiumSyncState<T>

    • The synchronized state object.

    Example

    // Define initial state
    type State = { count: number };
    const initialState: State = { count: 0 };

    // Use the hook in a component
    const MyComponent: React.FC = () => {
    const syncState = useSyncState<State>(initialState, 'increment');

    // Use the state in an event handler
    const handleClick = () => {
    syncState.set('count', syncState.get('count') + 1);
    syncState.dispatch('increment');
    };

    return (
    <div>
    <p>Count: {syncState.get('count')}</p>
    <button onClick={handleClick}>Increment</button>
    </div>
    );
    };

    *

    See

    ReactiumSyncState to understand the underlying synchronized state object.