Skip to content

Instantly share code, notes, and snippets.

View MehYam's full-sized avatar
💭
thinking

Kai J Arnold MehYam

💭
thinking
View GitHub Profile
@MehYam
MehYam / useManagedState.ts
Created August 26, 2024 17:38
A React managed state solution in 27 lines of code using useSyncExternalStore and no external dependencies
import * as React from 'react';
const events = new EventTarget();
type StateInstance<T> = {
subscribe: (callback: () => void) => (() => void),
getSnapshot: () => T,
setter: (t: T) => void,
data: T
}
const store: Record<string, StateInstance<any>> = {};