I have an issue where my state is only set on the second click. Ive been reading about lifting up the state which Ive tried to implement but I am still running into the same issue with this button. There are multiple components that the state is passed through and maybe thats the issue I could use some help understanding how to resolve this issue Ill try and outline in the simplest way:
I have my parent table component:
const TableView = ({...all the props}) => { const [sort, setSortBy] = useState({ sortBy: "", order: "", }); const handleOnsortChange = (values) => { setSortBy(values); trimData(items.sort(sorter), pageNumber, 50); }; // sort function const sorter = (a, b) => { ...} return ( <Table> <TableHeader handleOnsortChange={handleOnsortChange} sort={sort} /> ... ) Table Header Component:
const TableHeader = ({ handleOnsortChange, sort }) => { const onSortChange = (value) => { handleOnsortChange(value); }; return ( <thead className="payroll-list-header"> <tr> <th> <span>Name</span> <SortView onChange={onSortChange} type={"fullName"} sort={sort} /> <div className="arrow"></div> </th> ... ) Sorter Component:
const SortView = ({ onChange, type, sort: { sortBy, order } }) => { const onClick = (order) => { onChange({ sortBy: type, order }); }; return ( <div className="sort"> <div onClick={() => onClick("asc")} className="icon-up"> <Octicon icon={TriangleUp} className={`${order === "asc" && sortBy == type ? "active" : ""}`} ></Octicon> </div> <div onClick={() => onClick("desc")} className="icon-down"> <Octicon icon={TriangleDown} className={`${order === "desc" && sortBy == type ? "active" : ""}`} ></Octicon> </div> </div> ); }; UPDATE: TrimData & sorter function: outside of these functions the state for sort is updated but inside when i console sort its empty.
//sort providers const sorter = (a, b) => { const nameA = a.FullName.toUpperCase(); const nameB = b.FullName.toUpperCase(); let compare = 0; if (sort.sortBy === "fullName") { if (nameA > nameB) { compare = 1; } else if (nameA < nameB) { compare = -1; } if (sort.order === "asc") { return compare; } else if (sort.order === "desc") { return compare * -1; } }; const trimData = (items, pageNumber, rows) => { const pages = items.length > 0 ? Math.ceil(items.length / rows) : 1; const trimStart = (pageNumber - 1) * rows; const trimEnd = trimStart + rows; const trimedData = items.slice(trimStart, trimEnd); setEndRange(trimEnd); setTrimedData(trimedData); setNumberOfPages(pages); }; You can see sort is passed through to the sort component and set in the parent. Ive tried setting it in the descendant components but that didn't help. Also passing setSortBy offered the same result. I hope this isnt to big of a code chunk and someone can offer advice to a junior dev :)
Octicon) showing the rightorderandsortByvalues? If so, the state of thesortvalue is being set properly. Is the actual sorting itself not working? If so please provide detail on whattrimDatais doing and whatitemsare. I only ask because to me it looks like everything is being passed down in a way that it should update properly.trimDataandsorter. I will add them into the question