import { useEffect } from 'react' ; import isEqual from 'lodash/isEqual' ; import useGetPermissions from './useGetPermissions' ; import { useSafeSetState } from '../util/hooks' ; interface State { permissions ? : any ; error ? : any ; } const emptyParams = { } ; const alreadyFetchedPermissions = { '{}' : undefined } ; see usePermissions param { Object } params returns example const usePermissionsOptimized = ( params = emptyParams ) => { const key = JSON . stringify ( params ) ; const [ state , setState ] = useSafeSetState < State > ( { permissions : alreadyFetchedPermissions [ key ] , } ) ; const getPermissions = useGetPermissions ( ) ; useEffect ( ( ) => { getPermissions ( params ) . then ( permissions => { if ( ! isEqual ( permissions , state . permissions ) ) { alreadyFetchedPermissions [ key ] = permissions ; setState ( { permissions } ) ; } } ) . catch ( error => { setState ( { error , } ) ; } ) ; } , [ getPermissions , key ] ) ; return state ; } ; export default usePermissionsOptimized ;