I am trying to setState value from a callback function the problem is that the return BIM7AATypeComments sometimes is undefined but if I refresh the page it will add value to setState.
I am not sure if I should use new Promise((resolve, reject)=>{})
however, I am using useEffect to fire the function.
thanks!
//Get All Leavs Props
export const getAllLeavesProperties = (
allModels: Autodesk.Viewing.GuiViewer3D | undefined,
cb?
) => {
const allLoadedViewers = isolateAndColorObject(
allModels as Autodesk.Viewing.GuiViewer3D
);
return getAllLeavesDbId(allModels, (dbId: number[]) => {
const count = dbId.length;
dbId.forEach((id) => {
if (allLoadedViewers && id) {
for (let model of allLoadedViewers) {
if (model)
model.getProperties(id, (props) => {
for (let prop of props.properties) {
if (!prop) return;
if (!histogram[prop.displayName]) {
histogram[prop.displayName] = {};
}
if (!histogram[prop.displayName][prop.displayValue]) {
histogram[prop.displayName][prop.displayValue] = [];
}
histogram[prop.displayName][prop.displayValue].push(props.dbId);
}
});
}
}
});
cb(histogram);
});
};
const test = () => {
if (!allModels) return;
else {
getAllLeavesProperties(allModels, (data: any[]) => { //callback
if (!data) return;
//@ts-ignore
const BIM7AATypeComments = data["BIM7AATypeComments"];
if (BIM7AATypeComments) {
try {
//@ts-ignore
const modelData = data["BIM7AATypeComments"];
const label = Object.keys(modelData);
console.log(label);
if (label) setTt(label);
//@ts-ignore
const myObject = Object.keys(data["BIM7AATypeComments"]).map(
//@ts-ignore
(key) => data["BIM7AATypeComments"][key].length
);
if (myObject) setrr(myObject);
} catch (error) {
console.log(error);
}
}
});
}
};
/*
React.useEffect(() => {
test();
}, []);
*/