im trying to increment a value by 1. no matter what i try it adds more than 1. i know the function only gets called once, and only when a button is clicked
const [counter, setCounter] = useState(0)
const handleSubmit = () => {
setCounter(counter + 1)
console.log('handleSubmit is running', counter) //TO SHOW ME HOW MANY TIMES THE FUNCTION RUNS. ONLY INCREMENTS BY 1 EACH TIME I CLICK THE BUTTON
setLeveledChar(prevChar => {
let newChar = {...prevChar}
//INCRIMENTS BY 1
newChar.char_level++
//SELECTED CLASS IS DIFFERENT DEPENDING ON WETHER OR NOT ISmULTICLASING IS TRUE.
//EITHER THE SELECTED MULTICLASS OR LEVELEDcHAR.PRIMARY_CLASS
// BY CLASS I MEAN LIKE A ROLE PLAYING CLASS BTW. NOT A PROGRAMMING CLASS
const leveledClass = isMulticlassing ? newChar.multiclass.find(x => x.name === selectedClass.name)
:newChar.primary_class
//INCRIMENTS BY 2
leveledClass++
return newChar
})
}
i tried leveledClass.level = leveledClass.level + 1
leveledClass.level += 1
but they all add 2.