简体   繁体   中英

Cannot read property 'includes' of undefined in reactjs?

I try to resolve includes undefined, For that, I am using && operator.

 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;

  };

However, If I got data from API, this method is displayed can not read property undefined and when I am going to resolve 'includes' of undefined, I used && operator and I got true value.

 Cannot read property 'includes' of undefined. 

I don't need true value, I need false value for the initially mounted component.

my question is that how can I resolve Cannot read property 'includes' of undefined by using && operator or anything.
Here is my code sandbox: https://codesandbox.io/s/stackoverflow-a-60764570-3982562-v1-um18k

Instead of some, you can use every to check all.

isAllChecked = label => {
    const { permission } = this.state;
    const data = groupItems.every(
      x => permission && permission[label] && permission[label].includes(x)
    );
    return data;
  };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM