I am trying to create a DetailsList with sortable columns (similar as the example in the documentation here: https://uifabric.azurewebsites.net/#/controls/web/detailslist), but instead of a Class component I want to use a functional components and hooks.
The problem is that when the onColumnClick function is being executed, the "items" array is still empty.
This is basically the setup of my component:
const MyList= () => {
const [items, setItems] = useState([]);
const [columns, setColumns] = useState([]);
useEffect(() => {
const newItems = someFetchFunction()
setItems(newItems);
const newColumns= [
{
key: "column1",
name: "Name",
fieldName: "name",
onColumnClick: handleColumnClick,
},
{
key: "column2",
name: "Age",
fieldName: "age",
onColumnClick: handleColumnClick,
},
];
setColumns(newColumns);
}, []);
const handleColumnClick = (ev, column) => {
console.log(items); // This returns [] instead of a populated array.
};
return <DetailsList items={items} columns={columns} />;
};
The problem is that in the handleColumnClick
the items
returns an empty array. This is obiously a problem when you want to sort the items by a specific column.