import { useEffect, useState } from "react"; type State = Readonly>; interface Action { type: string; } type Listener = (ns: S, ps?: S) => void; interface Store { dispatch: (a: A) => void; getState: () => S; subscribe: (listener: Listener) => () => void; } export default function useSelector(store: Store): S { const [state, setState] = useState(store.getState()); useEffect(() => { const unsubscribe = store.subscribe((ns) => { setState(ns); }); return () => { unsubscribe(); }; }, []); return state; }