Skip to content

Instantly share code, notes, and snippets.

View theopomies's full-sized avatar
📱
Building & Shipping

Théo Pomies theopomies

📱
Building & Shipping
View GitHub Profile
@theopomies
theopomies / mini-zustand.ts
Created February 3, 2025 16:57
mini-zustand.ts
import { useSyncExternalStore } from "react";
export function createStore<T extends object>(init: StoreInit<T>): UseStore<T> {
const listeners = new Set<Listener>();
const setter: Setter<T> = (setState) => {
const newStoreProps =
typeof setState == "function" ? setState(state) : setState;
state = Object.freeze(Object.assign({}, state, newStoreProps));
listeners.forEach((listener) => listener());