Reputation: 119
**trying the following The button will first be disabled when the promise fires. It will remain disabled until the promise resolves, where it will then be enabled again. Please advise as I am stuck here and not sure whether its the right thing **
let [enabled] = useState(false);
const getTotal = event => {
enabled = false;
calTotal().then(validateResult => {
// enabled = true;
});
}
<Button
onClick={() => new Promise((resolve) => {
resolve({
getTotal();
})
})}>Get Total</Button>
Upvotes: 0
Views: 1472
Reputation: 4686
There are a few things to consider:
enabled
correctly - you need to use the function from the useState
hooktrue
in order to be able to click the buttonSee adjustments here:
let [enabled, setEnabled] = useState(true);
const getTotal = event => {
setEnabled(false)
calTotal().then(validateResult => {
setEnabled(true)
});
}
<Button disabled={!enabled} onClick={() => getTotal()}>Get Total</Button>
Upvotes: 1