-
-
Save ryanswrt/6090ed8e71ce0c5db31dcadf97e3bc7d 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
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