Skip to main content

Overview

The useWidgetState hook provides persistent state management across ChatGPT sessions.

Import

import { useWidgetState } from 'sunpeak';

Signature

function useWidgetState<T>(
  key: string,
  initialValue: T
): [T, (value: T) => void]

Parameters

key
string
required
Unique identifier for the state value
initialValue
T
required
Default value if no persisted state exists

Returns

Returns a tuple similar to useState:
  • [0]: Current state value
  • [1]: Function to update the state

Usage

import { useWidgetState } from 'sunpeak';

function Counter() {
  const [count, setCount] = useWidgetState('count', 0);

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
}