I have a simple problem here which I can’t figure out. I wanted to hide menus depending on the condition.
For example if status
contains at least one “Unlinked”. “All unlinked images” menu should appear. I did used .some
and I wonder why it doesn’t return a boolean.
Codesandbox is here Click here
JavaScript
x
16
16
1
const showDeleteAllInvalidButton = () => {
2
const productImages = products?.flatMap((product) =>
3
product.productImages.filter((image) => image?.status)
4
);
5
6
return productImages?.some((e) => e?.status === "Invalid");
7
};
8
9
const showDeleteAllUnlinkedButton = () => {
10
const productImages = products?.flatMap((product) =>
11
product.productImages.filter((image) => image?.status)
12
);
13
14
return productImages?.some((e) => e?.status === "Unlinked");
15
};
16
Advertisement
Answer
The methods do return a boolean. But in the menus
array you are assigning a function reference not the result –
JavaScript
1
2
1
show: showDeleteAllInvalidButton // function reference
2
show
is now assigned a reference to the function showDeleteAllInvalidButton
not the result of productImages?.some
. You need to invoke the functions when assigning –
JavaScript
1
2
1
show: showDeleteAllInvalidButton() // result of productImages?.some
2