Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

347
Views
MUI 5 List, ListItemButton does not onclick on component load

MUI 5 List, ListItemButton does not on-click on component load

I have a List which gets selected correctly (the 0 index) but the onCLick does not get triggered when I refresh the page and I cant seem to get it working, it works fine on hot-reload.

          <ListItemButton
            key={i}
            className={`click-item p-2 message-group-item`}
            selected={selectedItem === i}
            onClick={() => handleGroupClick(group, i)}
            autoFocus
            dense
            divider
            style={
                selectedItem === i
                    ? { backgroundColor: "var(--lc-pink)", color: "#fff" }
                    : { backgroundColor: "#fff" }
            }>
            {group.name.replaceAll(",", ", ").replaceAll("+", ", ").replaceAll("-", "")}
        </ListItemButton>

This is the api call that I make and once I get the data pass on the 0 index to the click handler to fetch the relevant data for that click

        const getAllMessageGroups = () => {
          axios
            .get(`${baseUrl}message-group`)
            .then((response) => {
                const data = response.data.data;
                  setAllMessageGroups(data);

            if (selectedItem === 0) {
                handleGroupClick(data[0], 0);
            }
        })
        .catch((error) => {
            console.log("message-group-error ", error.message);
        });
     };

The onClick function, simple updates the index and uses a key to fetch the data for that click

 const handleGroupClick = (group, selectedIndex) => {

    updateSelected(selectedIndex);

    if (allMessageGroups[1].id === "0") {
        allMessageGroups.splice(1, 1); // remove new message
    }

  
    getGroupMessageWithId(group._key);
    setSelectedMsgGroup([]);
};

Whats strange to me is that when I hard reload the page the onClick does not get triggered but when I save in the IDe the hot-reload triggers the onClick

Any suggestions much appreciated

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!