Help! Here the setAdmittedColumns is not filling with columns, the reason I am doing this is because I need admittedColumns to have all columns to exclude what is needed, the end result in this is that the column will be added because it is no longer in the exclude list.
It makes sense it my mind, but computer says no. Please help.
useEffect(() => {
const excluded = new Set([...columnReadExcludes]);
// Admitted columns is not updating with columns
setAdmittedColumns(columns);
setAdmittedObjects(objects);
admittedColumns ? setAdmittedColumns(admittedColumns.filter(column => !excluded.has(column))) : '';
setAdmittedObjects(prevObjects =>
prevObjects.map(obj => {
const newObj = { ...obj };
for (const prop of excluded) {
delete newObj[prop];
}
return newObj;
})
);
}, [objects, columnReadExcludes]);
The issue with the code is that setAdmittedColumns is being called twice, once with columns and then again with admittedColumns.filter(column => !excluded.has(column)). The second call is overwriting the first call and hence admittedColumns is not being updated with the filtered columns. To fix this, you can combine both calls into one by filtering the columns first and then passing it to setAdmittedColumns. Here's the updated code:
In this code, filteredColumns is first computed by filtering admittedColumns if it exists, otherwise columns is used. Then, setAdmittedColumns is called with filteredColumns.