For condition rendering, i'm checking if my object get an empty value.
I'm using "useState" to pass each object value to "isEmpty". First I'm creating a new object because i delete the value " SummaryOfChanges" who can be empty. After, I'm using "some" to get every value and pass every value to 'isEmpty'
//USE EFFECT
useEffect(() => {
if (offerData) {
let myObject = { ...offerData };
const { SummaryOfChanges, ...newObject } = myObject;
if (Object.values(newObject)?.some((x) => isEmpty(x))) {
setCheckIfEmpty(true);
} else {
setCheckIfEmpty(false);
}
}
}, []);
//ISEMPTY
export const isEmpty = (value) => {
return (
value === undefined ||
value === null ||
(typeof value === 'object' && Object.keys(value).length === 0) ||
Object.keys(value) === undefined ||
Object.keys(value) === null ||
(typeof value === 'string' && value.trim().length === 0)
);
};
exemple of my object :
offerData : {
valueOne : "hello",
objectOne : {
valueOne: 'Hello',
valueTwo : null}
}
problem : isEmpty work perfectly and check in my object if value, nested object or array are empty. But sometimes, nested object is not empty but have value "null".
I need to add in my "isEmpty" some condition to check each value in nested object and array.
So, in this situation, setCheckIfEmpty will return 'false' because my "objectOne" is not empty, even if "objectOne.valueTwo" === null.
I'm actually trying to map through each value, but for the moment it is not working.