Here is my code, I add a new text to the database and it works perfectly I can see it in my database, but when I want to update the display, despite I retrieve the new data from the database and update the state of the data with the latest data, it still shows the old data when I log it.
const submit = async (e) => {
e.preventDefault();
const code = e.target.elements['text'].value;
try {
addTextToDataBase(text);
console.log('text added to database');
const response = await fetch(url);
const data = await response.json();
setDataFromDB(data);
console.log(data);
} catch (e) {
console.log(e);
}
};
Here is the addCodeToDB function:
const addCodeToDB = async (code, percentage) => {
try {
await fetch('http://localhost:3001/api/codes', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
code: code,
percentage: percentage,
status: 'active',
}),
});
} catch (error) {
console.error(error);
}
};