Skip to content

"Index out of bounds" error #847

@jthemphill

Description

@jthemphill

I have code like this:

function MultiAllotment({...}) {
  return <Allotment
        ref={allotmentRef}
        vertical={split === 'vertical'}
        defaultSizes={defaultSizes}
        separator={!hideSeparator}
        onVisibleChange={(index, visible) => panes[index].onVisibleChange?.(visible)}
        onChange={(sizes) => panes.forEach((pane, index) => pane.onChange?.(sizes[index]))}
        onDragEnd={(sizes) => panes.forEach((pane, index) => pane.onDragEnd?.(sizes[index]))}
        className={styles.main}
      >
        {panes.map(({ id, visible, snap, preferredSize, minSize, maxSize, className, children }) => (
          <Allotment.Pane
            key={id}
            visible={visible}
            snap={snap}
            preferredSize={preferredSize}
            minSize={minSize}
            maxSize={maxSize}
            className={className}
          >
            {children}
          </Allotment.Pane>
        ))}
      </Allotment>
}

It's failing here:

if (index < 0 || index >= this.viewItems.length) {
throw new Error("Index out of bounds");
}

This is because we're calling isViewVisible() for each index in childrenArray:

splitViewRef.current?.isViewVisible(index) !== props.visible

...which means we'll throw an error if our SplitView has fewer viewItems than the childrenArray we passed to the Allotment. Whenever the exception is thrown, the SplitView has an empty viewItems array.

But... I don't know why we should expect SplitView to have the same number of viewItems as childrenArray, or why we shouldn't expect viewItems to be nonempty. Is there any guarantee that these arrays are the same size or that the viewItems will be nonempty? I can't find anything in the documentation that would imply that I'm using Allotment incorrectly.

If there is no guarantee, should we really be throwing an exception? This is a predicate function. Can it return false instead?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions