Home > Software design >  React Native I am having issues with adding a number from state
React Native I am having issues with adding a number from state

Time:01-08

react native is not increasing my number by 2 instead it keeps append new text to my state. I don't have a problem when subtracting the problem is just with adding

const [vol, setVol] = useState(10)

useEffect(()=>{
  setVol(e=> e   2)
},[])

console.log(vol)
// output 102

I need to increase the state vol (10) by 2 so output should be 12

CodePudding user response:

e will be a string and hence will follow string addition change it to Number before adding const [vol, setVol] = useState(10)

useEffect(()=>{
  setVol(e=> String(Number(e)   2))
},[])

console.log(vol)
// output 12
  •  Tags:  
  • Related