import React from 'react' ; import Store from './store' ; export default function useStore < T extends Store < any > > ( store ) { const [ , rerender ] = React . useState < any > ( ) ; const memoizedState = React . useMemo ( ( ) => store . state , [ store . state ] ) ; React . useEffect ( ( ) => { function syncState ( ) { if ( memoizedState !== store . state ) { rerender ( { } ) ; } } function onChange ( ) { syncState ( ) ; } syncState ( ) ; store . subscribe ( onChange ) ; function cleanup ( ) { store . unsubscribe ( onChange ) ; } return cleanup ; } , [ store ] ) ; return store ; }