I have this function in ReactJS. When user opens home page the good entries number from database is shown. But when I refresh page the counting starts from 0+1. Where is the mistake here?
const [user, setUser] = useState({ id: "", name: "", email: "", entries: 0, joined: "", });
const onButtonSubmit = () => {
setImageUrl(input);
fetch("http://localhost:3000/imageurl", {
method: "post",
headers: {
"Content-Type": "application/json",
Authorization: "bearer " + localStorage.getItem("token"),
},
body: JSON.stringify({
input: input,
}),
})
.then((response) => response.json())
.then((response) => {
if (response) {
setUser((prev) => {
localStorage.setItem(
"user",
JSON.stringify({
...JSON.parse(localStorage.getItem("user")),
entries: prev.entries + 1,
})
);
return { ...prev, entries: prev.entries + 1 };
});
}
displayPredictions(handleApiPredictions(response));
})
.catch((err) => console.log(err));
};