Hello i have this component and after some reading i have undestand that the useEffect is called again when data in array change but in my case that's not change and i get into a loop.
export default function Test() {
const [dates, setDates] = useState([]);
let values = [];
useEffect(() => {
getAllFilledJournauxDates().then(function(response) {
values = response;
setDates(values);
}
);
}
, [dates]);
console.log(values);
return(
<div>
{dates.map(date => {
return <div>{date}</div>
})}
</div>
);
}
[EDIT 2] adding the funtion getAllFilledJournauxDates()
export async function getAllFilledJournauxDates() {
let dates = [];
let journaux = getAllJournaux()
.then(response => {
Object.keys(response).forEach(element => {
dates.push(new Date(new Date(response[element].date)).getFullYear() + "-" + (new Date(response[element].date)).getMonth() + "-" + (new Date(response[element].date)).getDate());
})
})
return dates;
}
[EDIT]
export default function Test() {
const [dates, setDates] = useState([]);
let values = [];
useEffect(() => {
console.log("useEffect"); // displayed two time in console
getAllFilledJournauxDates().then(function(response) {
values = response;
setDates(values);
console.log(values); //display the array of dates
}
);
}
, []);
console.log(values); // display an empty array
return(
<div>
{dates.map(date => {
return <div>{date}</div> // display nothing on screen
})}
</div>
);
}
Like suggered by @rowinbot i have removed the variable in array , but nothing yet on the screen, i can see that the variable is filled in my console, and the useEffect look like he is called two times ... i want to add that at first i try to fill a calendar with those values but without get the result i wanted i haved render the result most simply