-
-
Save SaiEashwarKS/032768adaa9f02de24d85fe432469551 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef } from 'react'; | |
import { Hook } from './singletonHookTypes'; | |
export const SingleItemContainer = ({ initValue, useHookBody, applyStateChange, args = [] }: Hook) => { | |
const lastState = useRef(initValue); | |
if (typeof useHookBody !== 'function') { | |
throw new Error(`function expected as hook body parameter. got ${typeof useHookBody}`); | |
} | |
const hookReturn = useHookBody(...args); | |
useEffect(() => { | |
//apply change to consumers if value has changed | |
if (lastState.current !== hookReturn) { | |
lastState.current = hookReturn; | |
applyStateChange(hookReturn); | |
} | |
}, [applyStateChange, JSON.stringify(hookReturn)]); //eslint-disable-line react-hooks/exhaustive-deps | |
return null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment