Not able to get inside the for loop in React js

I am making a To-Do App. I am using React frontend, Node backend, and Postgresql DB.
I made a component called <TaskLists /> which takes three properties or props:

  1. startDate
  2. endDate
  3. allTasks

allTasks is an array of task lists. Its basic structure is as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>allTasks = [
{
id: //taskList id
list_date: //taskList date
tasks: [
{
id: // task id
title: // task title
done: // boolean value either true or false
}, ...
]
}, ...
]
</code>
<code>allTasks = [ { id: //taskList id list_date: //taskList date tasks: [ { id: // task id title: // task title done: // boolean value either true or false }, ... ] }, ... ] </code>
allTasks = [
    {
        id: //taskList id
        list_date: //taskList date
        tasks: [
            {
                id: // task id
                title: // task title
                done: // boolean value either true or false
            }, ...
        ]
    }, ...
]

I am getting this allTasks array from my backend successfully as I can see from my react dev tool.

Using startDate and endDate, I am rendering all the task lists with their list_date b/w the two using the <TaskLists /> component.

I am trying to render today’s tasks (i.e. 19-07-2024) by the following code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><TaskLists startDate={props.currentDate} endDate={props.currentDate} currentDate={props.currentDate} allTasks={props.allTasks} />
</code>
<code><TaskLists startDate={props.currentDate} endDate={props.currentDate} currentDate={props.currentDate} allTasks={props.allTasks} /> </code>
<TaskLists startDate={props.currentDate} endDate={props.currentDate} currentDate={props.currentDate} allTasks={props.allTasks} />

I made the startDate and endDate property same to currentDate (i.e. 19-07-2024) which I am getting from a parent component.

Now inside my <TaskLists /> component, I want to render either the list of tasks having list_date b/w startDate and endDate or I want to render a <TaskListAbsent /> component which renders if there is no task list b/w startDate and endDate.

So I made a useState() hook:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const [requiredTaskLists, setRequiredTaskLists] = useState([]);
</code>
<code>const [requiredTaskLists, setRequiredTaskLists] = useState([]); </code>
const [requiredTaskLists, setRequiredTaskLists] = useState([]);

Following is the code to set the variable requiredTaskLists:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>const changeRequiredTaskLists = () => {
let newTaskList = [];
for(let i=0; i<props.allTasks.length; i++) {
if(props.startDate <= props.allTasks[i].list_date && props.endDate >= props.allTask[i].list_date) {
newTaskList.push(allTasks[i]);
}
}
setRequiredTaskLists(newTaskList);
}
</code>
<code>const changeRequiredTaskLists = () => { let newTaskList = []; for(let i=0; i<props.allTasks.length; i++) { if(props.startDate <= props.allTasks[i].list_date && props.endDate >= props.allTask[i].list_date) { newTaskList.push(allTasks[i]); } } setRequiredTaskLists(newTaskList); } </code>
const changeRequiredTaskLists = () => {
    let newTaskList = [];
    for(let i=0; i<props.allTasks.length; i++) {
      if(props.startDate <= props.allTasks[i].list_date && props.endDate >= props.allTask[i].list_date) {
        newTaskList.push(allTasks[i]);
      }
    }
    setRequiredTaskLists(newTaskList);
  }

And then I called the changeRequiredTaskLists() function in a useEffect() hook to call that function with every render:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>useEffect(() => {
changeRequiredTaskLists();
}, []);
</code>
<code>useEffect(() => { changeRequiredTaskLists(); }, []); </code>
useEffect(() => {
    changeRequiredTaskLists();
}, []);

And lastly, I am rendering the <TaskLists /> component as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>return (
<>
{
requiredTaskLists.length !== 0 ?
<>
{
requiredTaskLists.map((taskList) => {
// code for displaying every `taskList` from `requiredTaskLists` array.
})
}
</> :
<>
<TaskListAbsent />
</>
}
</>
)
</code>
<code>return ( <> { requiredTaskLists.length !== 0 ? <> { requiredTaskLists.map((taskList) => { // code for displaying every `taskList` from `requiredTaskLists` array. }) } </> : <> <TaskListAbsent /> </> } </> ) </code>
return (
    <>
      {
        requiredTaskLists.length !== 0 ? 
        <>
          {
            requiredTaskLists.map((taskList) => {
              // code for displaying every `taskList` from `requiredTaskLists` array.
            })
          }
        </> : 
        <>
          <TaskListAbsent />
        </>
      }
    </>
  )

But the problem is that for loop inside checkRequiredTaskLists is not getting executed. And I can confirm that with console logs.
Console logs just before and after for loop are getting executed and but console logs inside for loop are not getting executed.

And therefore, the newTaskList array remains empty [] and requiredTaskLists gets set to an empty array too []. Hence, requiredTaskLists.length always equals to 0.

And hence my code for displaying list is not rendering at all. It is always the <TaskListAbsent /> component which is rendering.

Please help me with this. Tell me where I am wrong. And is there is another way to achieve what I am trying to achieve?

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật