How to calculate percentage on state change in react hooks

i am having the below state

const [data,setData] = useState({maths:null,physics:null,chem:null,percentage:null})

and i was updating the state as

setData({...data,[e.target.name]:e.target.value})

and my fuction for calculating the percentage is

function percentage(data.maths,data.physics,data.chem){ 
     const per = ((data.maths+data.physics+data.chem)/100)*100
return per
}

my doubt is when to call this function(like useEffect or like normal) and store it in data.percentage
please help me

6 thoughts on “How to calculate percentage on state change in react hooks”

Leave a Comment