Skip to content

createStorage

Creates new Storage object.

Import

import { createStorage } from "starkweb/react"

Usage

import { createStorage } from "starkweb/react"
 
const storage = createStorage({ storage: localStorage })

Parameters

import { type CreateStorageParameters } from "starkweb/react"

deserialize

(<T>(value: string) => T) | undefined

  • Function to deserialize data from storage.
  • Defaults to deserialize.
import { createStorage, deserialize } from "starkweb/react"
 
const storage = createStorage({
  deserialize, 
  storage: localStorage,
})

key

string | undefined

  • Key prefix to use when persisting data.
  • Defaults to starkweb.
import { createStorage } from "starkweb/react"
 
const storage = createStorage({
  key: "my-app", 
  storage: localStorage,
})

serialize

(<T>(value: T) => string) | undefined

  • Function to serialize data for storage.
  • Defaults to serialize.
import { createStorage, serialize } from "starkweb/react"
 
const storage = createStorage({
  serialize, 
  storage: localStorage,
})

storage

{ getItem(key: string): string | null | undefined | Promise<string | null | undefined>; setItem(key: string, value: string): void | Promise<void>; removeItem(key: string): void | Promise<void>; }

  • Storage interface to use for persisting data.
  • Defaults to localStorage.
  • Supports synchronous and asynchronous storage methods.
import { createStorage } from "starkweb/react"
// Using IndexedDB via https://github.com/jakearchibald/idb-keyval
import { del, get, set } from "idb-keyval"
 
const storage = createStorage({
  storage: { 
    async getItem(name) { 
      return get(name)
    }, 
    async setItem(name, value) { 
      await set(name, value) 
    }, 
    async removeItem(name) { 
      await del(name) 
    }, 
  }, 
})

Return Type

import { type Storage } from "starkweb/react"

Storage

Object responsible for persisting Starkweb State and other data.

import { type Storage } from "starkweb/react"

getItem

getItem(key: string, defaultValue?: value | null | undefined): value | null | Promise<value | null>

import { createStorage } from "starkweb/react"
 
const storage = createStorage({ storage: localStorage })
const recentConnectorId = storage.getItem("recentConnectorId") 

setItem

setItem(key: string, value: any): void | Promise<void>

import { createStorage } from "starkweb/react"
 
const storage = createStorage({ storage: localStorage })
storage.setItem("recentConnectorId", "foo") 

removeItem

removeItem(key: string): void | Promise<void>

import { createStorage } from "starkweb/react"
 
const storage = createStorage({ storage: localStorage })
storage.removeItem("recentConnectorId")