I am trying to use a select element to set internal state to either ‘ascending’ or ‘descending’. Once the state is set, I would like to use that state value to conditionally sort A-Z or Z-A with orderBy from the lodash package. I know the sort method works because I have console logged them, but for some reason my handleSort function is not sorting my array of items at all.
Here is my code:
import { useState } from "react";
import { orderBy } from "lodash";
const Subjects = () => {
const { data } = useQuery(GET_SUBJECTS);
const [sortState, setSortState] = useState("-");
let sortedSubjects = data.subjects;
const handleSort = (e) => {
setSortState(e.target.value);
if (sortState === "ascending") {
sortedSubjects = orderBy(data.subjects, "name", "asc");
} else if (sortState === "descending") {
sortedSubjects = orderBy(data.subjects, "name", "desc");
}
};
return (
<>
<select
onChange={handleSort}
defaultValue="default"
className="home-page-filter"
>
<option value="default">-</option>
<option value="ascending">A-Z</option>
<option value="descending">Z-A</option>
</select>
<h2>Subject</h2>
<div className="subjects-container">
{sortedSubjects.map((subject) => (
<SubjectRow key={subject.id} subject={subject} />
))}
</div>
</>
);
};
export default Subjects;
I’ve tried putting the sort function directly in the JSX wondering if it was some sort of hoisting issue. I’m still a junior dev, so it might be something very simple and somewhat obvious that I’m missing or doing wrong here. Thank you for any help!
2
Answers
Move the if block outside of handleSort and into a useEffect hook:
see the react docs for useEffect
The
sortedSubjects
is computed usingdata
andsortState
. This means that you need to compute it on each render.Compute
sortedSubjects
each the body of the component. Whenever the component is re-rendered it would be re-computed. If you want to avoid doing it on each render, wrap it withuseMemo
.In addition, instead use
asc
anddesc
as the options value, which would shorten you code a bit (see comments in code).Because you have the array (
data
) and thesortState