import {useEffect, useRef, useState} from 'react'; import type {Store} from 'reflux'; import type {CommonStoreDefinition} from './types'; interface LegacyStoreShape extends Store, CommonStoreDefinition {} /** * Returns the state of a reflux store. Automatically unsubscribes when destroyed * * ``` * const teams = useLegacyStore(TeamStore); * ``` */ export function useLegacyStore( store: T ): ReturnType { const [state, setState] = useState(store.getState()); // Not all stores emit the new state, call get on change const callback = () => setState(store.getState()); // If we setup the listener in useEffect, there is a small race condition // where the store may emit an event before we're listening (since useEffect // fires AFTER rendering). Avoid this by ensuring we start listening // *immediately* after we initialize the useState. const unlisten = useRef(); if (unlisten.current === undefined) { unlisten.current = store.listen(callback, undefined); } useEffect(() => { return () => void unlisten.current?.(); }, []); return state; }