Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Table not re -rendering after sorting useState object

const [members, setMembers] = useState([]);
  useEffect(() => {
    getMembers();
  }, []);

  const sortByName = () => {
    const sorted = members.sort((a, b) => {
      if (a.name < b.name) {
        return -1;
      }
      if (a.name > b.name) {
        return 1;
      }
      return 0;
    }
    );
    setMembers(sorted)
  }

{members.map((person) => {
return (
<Member key={person.member_id} person={person}/>
)})}

The table is not re-rendered with the sorted data after the sortByName function is called, console logging the member variable gives proper sorted data, but the table still displays the old data

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I think sort method doesnt return a new array. So maybe u need to try :

setMembers([...sorted])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading