Skip to content

createStorage

Creates new Storage object.

Import

ts
import { createStorage } from 'wagmi'

Usage

ts
import { createStorage } from 'wagmi'

const storage = createStorage({ storage: localStorage })

Parameters

ts
import { type CreateStorageParameters } from 'wagmi'

deserialize

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

  • Function to deserialize data from storage.
  • Defaults to deserialize.
ts
import { createStorage, deserialize } from 'wagmi'

const storage = createStorage({
  deserialize, 
  storage: localStorage,
})

WARNING

If you use a custom deserialize function, make sure it can handle bigint and Map values.

key

string | undefined

  • Key prefix to use when persisting data.
  • Detaults to 'wagmi'.
ts
import { createStorage } from 'wagmi'

const storage = createStorage({
  key: 'my-app', 
  storage: localStorage,
})

serialize

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

  • Function to serialize data for storage.
  • Defaults to serialize.
ts
import { createStorage, serialize } from 'wagmi'

const storage = createStorage({
  serialize, 
  storage: localStorage,
})

WARNING

If you use a custom serialize function, make sure it can handle bigint and Map values.

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.
ts
import { createStorage } from 'wagmi'
// 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

ts
import { type Storage } from 'wagmi'

Storage

Object responsible for persisting Wagmi State and other data.

ts
import { type Storage } from 'wagmi'

getItem

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

ts
import { createStorage } from 'wagmi'

const storage = createStorage({ storage: localStorage })
const recentConnectorId = storage.getItem('recentConnectorId')

setItem

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

ts
import { createStorage } from 'wagmi'

const storage = createStorage({ storage: localStorage })
storage.setItem('recentConnectorId', 'foo')

removeItem

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

ts
import { createStorage } from 'wagmi'

const storage = createStorage({ storage: localStorage })
storage.removeItem('recentConnectorId')

Released under the MIT License.