Skip to content

Instantly share code, notes, and snippets.

@aidangarza
Created March 9, 2021 17:43
Show Gist options
  • Save aidangarza/cf315ca14ec139bf54f577e8ec9a0790 to your computer and use it in GitHub Desktop.
Save aidangarza/cf315ca14ec139bf54f577e8ec9a0790 to your computer and use it in GitHub Desktop.
A hook for locking access to any subject to prevent multiple components from overwriting a piece of shared state
// In the style of useRef, the locks variable has a
// current property which is an object that will
// store the current lockout state of different features
const locks = {
current: {}
};
export default function useLockout(feature) {
const lockout = (shouldLock = true) => {
locks.current[feature] = shouldLock;
};
return [
() => locks.current[feature],
lockout
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment