Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React Native I am having issues with adding a number from state

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading