@-ft/use-value-persist
v0.1.0
Published
React useValuePersist hook
Downloads
6
Readme
use-value-persist - react useValuePersist
hook
use persist to prevent unnecessary re-attach event handlers
Usage
import { useValuePersist } from '@-ft/use-value-persist';
function MyComponent() {
const { enabled } = useContext(MyContext);
const enabledPersist = useValuePersist(enabled);
const handlePress = useCallback(() => {
if (enabledPersist.current) alert('button is pressed when enabled');
}, [enabledPersist]);
// instead of below which will cause re-attach event handler
const DONT_DO_THIS_badHandlePress = useCallback(() => {
if (enabled) alert('button is pressed when enabled');
}, [enabled]);
return <button onPress={handlePress}>Click me!</button>;
}