[英]Cannot read property 'includes' of undefined in reactjs?
我嘗試解決包含未定義的問題,為此,我使用 && 運算符。
isAllChecked = label => {
const { permission } = this.state;
false value - I need false value when I get data from API,
But I got an error that includes undefined. for that, I used && operator
but I got a true value, I don't need to get true value
const data = !groupItems.some(
x => !permission && !permission[label] && !permission[label].includes(x)
);
// console.log(true) //true
const data = !groupItems.some(
x => !permission[label].includes(x)
);
// I got a false value using static data without using && operator
// data output: false (accepted output- getting using static data, but I need
to get the same value when I get data from API, I got an error includes undefined without using && operator)
return data;
};
但是,如果我從 API 獲取數據,則顯示此方法無法讀取未定義的屬性,並且當我要解析未定義的“包含”時,我使用了 && 運算符並獲得了真值。
Cannot read property 'includes' of undefined.
我不需要真值,我需要初始安裝組件的假值。
我的問題是如何解決無法使用 && 運算符或其他任何內容讀取未定義的屬性“包含”。
這是我的代碼沙箱: https : //codesandbox.io/s/stackoverflow-a-60764570-3982562-v1-um18k
您可以使用 every 來檢查所有內容,而不是一些。
isAllChecked = label => {
const { permission } = this.state;
const data = groupItems.every(
x => permission && permission[label] && permission[label].includes(x)
);
return data;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.