Playing with Fluent UI DetailsList, I've created this solution. I've deleted all unnecessary lines to minimize the problem, as it appears that when logging in onColumnClick
then somehow it doesn't print the current state but the initial state, why is this happening:
import * as React from 'react';
export const ClvKkcgListyHistorie = (props: IClvKkcgListyHistorieWebPartProps) => {
const [notifications, setNotifications] = React.useState<INotificationDetailsList[]>([]);
const [columns, setColumns] = React.useState<IColumn[]>();
React.useEffect(() => {
setColumns(createColumns(onTitleClick, onColumnClick));
}, []);
// With this useEffect everything works fine
React.useEffect(() => {
setColumns(createColumns(onTitleClick, onColumnClick));
}, [notifications]);
React.useEffect(() => {
const fetchAllNotifications = async () => {
// Fetch items here
}
fetchAllNotifications();
}, [])
const onColumnClick = (e: any, column: IColumn) => {
console.log(notifications,columns, "1" );
// Here I get [], undefined, "1"
}
return (
<>
{notifications.length !== 0 &&
<DetailsList
items={notifications}
columns={columns}
>
</DetailsList>
}
</>
);
}
Columns.tsx
import * as React from 'react';
export const createColumns = (onTitleClick: any, onColumnClick: any) : IColumn[] => {
return [
{
key: "title",
name: "Nadpis",
minWidth: 70,
maxWidth: 100,
isResizable: true,
isRowHeader: true,
isSorted: false,
isSortedDescending: false,
sortAscendingAriaLabel: 'A - Z',
sortDescendingAriaLabel: 'Z - A',
isPadded: true,
onColumnClick: onColumnClick,
onRender: (item: INotificationDetailsList) => {
return <span>{item.Title}</span>;
}
},
}
]
}
In that