Skip to content

Instantly share code, notes, and snippets.

@ryanswrt
Created July 25, 2019 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanswrt/6090ed8e71ce0c5db31dcadf97e3bc7d to your computer and use it in GitHub Desktop.
Save ryanswrt/6090ed8e71ce0c5db31dcadf97e3bc7d to your computer and use it in GitHub Desktop.
const MyComp = () => {
const [permission, currentCount, setCount] = usePermissionedCounter(count);
const count = () => setCount(currentCount + 1);
return (<div> To infinity! {currentCount} <button onClick={count}>Count</button></div>);
}
const callBackendForPermission = () => new Promise((resolve) => resolve(true));
const usePermissionedCounter = (countCb) => {
const [currentCount, setCount] = useState(0);
const [permission, setPermission] = useState(true);
useEffect(() => {
// I decide if you may count
callBackendForPermission(currentCount).then((backendPermission) => {
if (backendPermission) {
countCb();
setPermission(true);
} else {
setPermission(false);
}
})
}, [currentCount, countCb])
return [permission, currentCount, setCount]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment