Overview
The useWidgetState hook provides persistent UI state management across ChatGPT sessions.
Import
import { useWidgetState } from 'sunpeak';
Signature
function useWidgetState<T>(
defaultState: T | (() => T)
): [T, (state: T | ((prevState: T) => T)) => void]
Parameters
Default value or function that returns default value. Used if no persisted state exists.
Returns
[1]
(state: T | ((prevState: T) => T)) => void
Function to update the state. Can accept a new state value or a function that receives the previous state and returns the new 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>
);
}