New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

jotai-persistence

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jotai-persistence

> :warning: **DRAFT**: Not intended for use yet

latest
Source
npmnpm
Version
2.0.5
Version published
Maintainers
0
Created
Source

Jotai Persistence

:warning: DRAFT: Not intended for use yet

Install

npm install jotai-persistence

About

Proivides an improved version of atomWithStorage that utilizes extended-serializer to serialize complex data, including nested atoms, and adds validators to validate serialized data.

Examples

import { createStorage } from "jotai-persistence";

const storage = createStorage(window.localStorage);

window.addEventListener("storage", (event: StorageEvent) => {
  if (event.storageArea === window.localStorage) {
    storage.set(event.key, storage.parse(event.newValue));
  }
});

const todos = storage.atom({
  key: "todos",
  initialValue: new Map<string, PersistentAtom<Todo>>(),
  validator: z.map(
    z.string(),
    atomSchema((key) => key[0] === "todo")
  ),
});

const addTodo = (title: string) => {
  const id = createId();
  const atom = storage.atom(["todo", id], title, storage);
  store.set(todos, (arr) => new Map([...arr, [id, atom]]));
};

Async storage with custom serialization transform:

import { createSerializer } from "jotai-persistence";
import { makeClassTransform } from "extended-serializer";
import { z } from "zod";

class Custom {
  constructor(
    public a: number,
    public b: number
  ) {}
}

const serializer = createSerializer([
  makeClassTransform({
    class: Custom,
    encode: (value) => [value.a, value.b],
  }),
]);

const storage = serializer.createStorage(AsyncStorage);

const atom = storage.atom<Custom[]>({
  key: "custom",
  initialValue: [],
  validator: z.array(z.instanceof<Custom>()),
});

Keywords

react

FAQs

Package last updated on 07 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts