Consider this scenario:
function myfunc() {
var total = 0;
const [ hooka, setHooka ] = useState([]);
const fetchData = () => {
api.get().then((data) => {
total = data.total;
setHooka(data.value);
}
}
return (
<h1>{total}</h1>
);
}
But the total inside the callback scope is different to the one defined in myfunc
scope.
How can I set value of total
in myfunc scope within the callback?
PS: It is a react hook in actual. Thank you TJ Crowder for the comment. And I've used total = data before a useState trigger method. I can use total as a hook also. But Doesn't creating more hooks slow down rendering in react or what?