NativeBase icon indicating copy to clipboard operation
NativeBase copied to clipboard

Accordion Breaks if Expanded is not passed as props

Open chakflying opened this issue 5 years ago • 2 comments

Issue Description

Just upgraded to the latest version and found my app crashing. Took a look at the code and seems to have found the problem.

Accordion.js:176

    const { expanded, expandMultiple } = this.props;
    // eslint-disable-next-line no-unused-vars
    let selected;
    if (expanded !== undefined && expanded !== null) {
      selected = Array.isArray(expanded) ? expanded : [expanded];
      selected = expandMultiple ? selected : selected.slice(0, 1);
    }
    this.state = {
      selected: props.expanded
    };

If expanded is not passed, state selected gets set to undefined or something. This cause the line below to fail, because we cannot get indexOf of undefined.

Accordion.js:231

    renderItem={({ item, index }) => (
          <AccordionItem
            disable={disable === index}
            key={String(index)}
            item={item}
            expanded={this.state.selected.indexOf(index) !== -1}

:184 looks like an obvious bug to me, but I'm not sure since there is a line explicitly disabling eslint. Sorry if I got things wrong. For now my workaround is to pass an empty array to the expanded prop.

     <Accordion
            dataArray={this.state.mydata}
            animation={true}
            expanded={[]}

chakflying avatar Jan 12 '21 04:01 chakflying

+1 Have the same problem today after upgrade to 2.15.2

Just add Array to expanded prop is all that you need to fix it:

Original example from Doc:

1610460495206

The right example:

1610460527980

jedi2209 avatar Jan 12 '21 14:01 jedi2209

Got the same problem and also laugh out loud when realized that other errors were caused by passing number (as the type asks) to "expanded" prop. The component actually expects an array but the interface requires a number.

marcoshevaristo avatar Jan 23 '21 15:01 marcoshevaristo