For the complete documentation index, see llms.txt. This page is also available as Markdown.

useCachedState

Hook which returns a stateful value, and a function to update it. It is similar to React's useState but the value will be kept between command runs.

The value needs to be JSON serializable.

Signature

function useCachedState<T>(
  key: string,
  initialState?: T,
  config?: {
    cacheNamespace?: string;
  },
): [T, (newState: T | ((prevState: T) => T)) => void];

Arguments

  • key is the unique identifier of the state. This can be used to share the state across components and/or commands (hooks using the same key will share the same state, eg. updating one will update the others).

With a few options:

  • initialState is the initial value of the state if there aren't any in the Cache yet.

  • config.cacheNamespace is a string that can be used to namespace the key.

  • config.cacheWriteDebounce is a number to set if you want to debounce the cache writes. This can be useful to prevent memory pressure.

Example

Last updated