I have a problem with two functions the problem is should click twice on the button to make a change and should click one time only
const addToBalance = (personId) => {
userInfo.map((user) => (
user._id === personId ? setUserGetMoney(user.balance) : ''
))
if (userGetMoney !== 0 && parseInt(inputMoney) !== 0 && parseInt(inputMoney) !== null){
setUserGetMoney(userGetMoney + parseInt(inputMoney))
console.log(userGetMoney)
}
}
const removeFromBalance = () => {
const id = params.id;
userInfo && userInfo.map((user) => (
user._id === id ? setUserRemoveMoney(user.balance) : ''
))
if (userRemoveMoney !== 0 && personId && parseInt(inputMoney) !== 0 && parseInt(inputMoney) !== null){
setUserRemoveMoney(userRemoveMoney - parseInt(inputMoney))
console.log(userRemoveMoney)
}
}
submit function
const onSubmitForm = (e) => {
e.preventDefault();
addToBalance(personId);
removeFromBalance();
if (personId && parseInt(inputMoney) !== 0 && parseInt(inputMoney) !== null){
postTransactionData();
}
}
<button type='submit' onClick={(e) => onSubmitForm(e)}>Submit</button>
When I try to click the submit button to execute the two functions addToBalance
and removeFromBalance
they don't work but when I click again on the submit button it works I want a solution so that I only click once on the button to make the functions work
the video explains the problem: https://drive.google.com/drive/folders/18usf6CXOyaDi4Py_VbEeNYdyE5Sl91-T?usp=sharing