itgirl1234
itgirl1234

Reputation: 749

Return isn't executed - Javascript, React.js

I've got a problem with my function, after calling it inside an if statement it works sometimes but it doesn't return the last return it starts again with new value.

Here's data I render:

 listData: [
    [
      {id: 1, title: 'list1'},
      {id: 2, title: 'list2'},
    ],
    {id: 3, title: 'list3'},
  ];

And function:

  isArray(a) {
    return (!!a) && (a.constructor === Array);
  }

  renderList(item, options) {
    if (this.isArray(item) === true) {
      item.map((child, childIndex) => {
        this.renderList(child, options)
      })
    }
    return (
      <List item={item} />
    )
  }

Upvotes: 1

Views: 122

Answers (2)

Dananjaya Ariyasena
Dananjaya Ariyasena

Reputation: 606

renderList(item, options) {
    if ( Array.isArray(item) ) {
      return item.map( (child, childIndex) => {
        return this.renderList(child, options)
      })
    } else {
      return (
         <List item={item} />
      )
    }
}

Upvotes: 3

Sergii Vorobei
Sergii Vorobei

Reputation: 1497

Please try this:

if (this.isArray(item) === true) {
  return item.map((child, childIndex) => {
    return this.renderList(child, options)
  })
}

Upvotes: 1

Related Questions