chat-plugin-sdk/docs/api/use-plugin-state.md
2023-11-22 15:31:48 +08:00

2.0 KiB

title description nav order group apiHeader
usePluginState Used to retrieve and update the running state of the plugin API 11 Hooks
pkg
@lobehub/chat-plugin-sdk/client

Used to retrieve and update the running state of the plugin.

Syntax

const [value, updateValue] = usePluginState<T>(key, initialValue);

Parameters

Parameter Type Description
key string Unique identifier for the state
initialValue T Initial value of the state

Return value

Properties Type Description
value T Current value of the state
updateValue (value: T) => void Function to update the state

Example

import { usePluginState } from '@lobehub/chat-plugin-sdk/client';

const Demo = () => {
  const [count, setCount] = usePluginState('count', 0);

  const increment = () => {
    setCount(count + 1);
  };

  return (
    <div>
      <h1>当前计数值:{count}</h1>
      <button onClick={increment}>增加</button>
    </div>
  );
};

export default Demo;

Notes

  • Make sure to use within a React function component.usePluginStateThe parameter must be of type string and is used to uniquely identify the plugin state.
  • keyThe parameter is the initial value of the state.
  • initialValueRepresents the current state value, obtained through destructuring assignment.
  • valueIs the function to update the state value, which accepts a new value as a parameter.
  • updateValueUsage Example

In the example above, we use 'usePluginState' to manage the state of a counter. The initial value is 0, and each click of the button increases the counter value by 1.

Related LinksusePluginStateReact Hook Documentation

相关链接