I have a problem that I can't solve, when I filter I have an infinite loop that starts I don't know why the problem is that when i choose the order of the day option to filter the data keeps loading in the console. Who can explain to me please
const dispatch = useDispatch()
const ifIsCommand = useSelector(state=> state?.reducerCommandAdmin?.state)
const [listCommand, setListCommand ] = useState([])
const [ filterDate, setFilterDate ] = useState("all");
useEffect(()=>{
setListCommand(ifIsCommand)
getAllCommandAllClientsAdminService(dispatch)
},[listCommand, filterDate])
const handleChange = (e) =>{
setFilterDate(e.target.value)
}
const ResultChoiceDateCommand = () =>{
if (filterDate === "oldDay") {
const sortListCommandOld = listCommand?.sort((a, b) => new Date(a.dateDuJour) - new Date(b.dateDuJour))
return setListCommand(sortListCommandOld)
}
if (filterDate === "recentDate") {
const sortListCommandOld = listCommand?.sort((a, b) => new Date(b.dateDuJour) - new Date(a.dateDuJour))
return setListCommand(sortListCommandOld)
}
if (filterDate === "dateOfDay") {
if(filterListCommandOfDay.length > 0){
return setListCommand(filterListCommandOfDay)
}else{
return <p>no order</p>
}
}
}
const year = new Date().getFullYear()
const day = new Date().getDate()
const newDay = day < 10 ? "0" + day : day
const month = new Date().getMonth() + 1
const newMonth = month < 10 ? "0" + month : month
const dateOfDay = `${year}-${newMonth}-${newDay}`
const filterListCommandOfDay = listCommand?.filter((dateOfDays) => {
if (dateOfDays.dateDuJour === dateOfDay) {
return dateOfDays.dateDuJour === dateOfDay
}
})