跳到内容
+

useSessionStorageState API

useSessionStorageState hook 的 API 参考。

Import

import useSessionStorageState from '@toolpad/core/useSessionStorageState';
// or
import { useSessionStorageState } from '@toolpad/core';

阅读这篇关于最小化 bundle 大小的指南,了解它们之间的区别。

Reference

useSessionStorageState

const [state, setState] = useSessionStorageState('my-key', 'initial value');

参数

  • key: string | null 用于在 window.sessionStorage 中存储值的键。
  • initialValue: T | null | () => T 当在 window.sessionStorage 中未找到 key 时返回的值。该值可以通过为此参数提供函数来延迟计算。
  • options?: object 此 hook 的其他选项。
    • codec?: Codec<T> 一种编解码器,可以将 V 类型的值编码和解码为字符串以及从字符串解码。
      • parse: (raw: string) => T 将字符串值解码为 V 类型的值。
      • stringify: (value: T) => string 将 V 类型的值编码为字符串。

返回

[T | null, React.Dispatch<React.SetStateAction<T | null>>] 类似于 React.setState 的结果,它返回一个元组,其中第一个项目表示状态,第二个项目表示状态的设置器。