I want to call some function whenever a user enters some value in text boxes. Here is my code using React Hooks:
function MyApp() {
const [name, setName] = React.useState("");
const [city, setType] = React.useState("");
const [completion, setCompletion] = useState(0);
calculateProgress = () => {
let v1 = name;
let v2 = city;
let total = 0;
if(v1.length >0 ) total += 50;
if(v2.length >0 ) total += 50;
setCompletion(total);
}
return {
<div>
<input type="name" onChange={(e) => setName(e.target.value); calculateProgress();}
<input type="city" onChange={(e) => setCity(e.target.value); calculateProgress();}
</div>
}
}
When I enter some value in name or city calculateProgress
should be called. But in my case, the value for name or city is empty inside calculateProgress
method whenever I type something. What is the correct way to do this?