static getDerivedStateFromProps()

in src/list/list.tsx [165:206]


  static getDerivedStateFromProps(nextProps: ListProps, prevState: ListState) {
    const {prevActiveIndex, prevData, activeItem} = prevState;
    const {data, activeIndex, restoreActiveIndex} = nextProps;
    const nextState = {prevActiveIndex: activeIndex, prevData: data};

    if (data !== prevData) {
      // Only clear activeIndex if it's out of bounds OR the item at that position changed
      const currentActiveIndex = prevState.activeIndex;
      const currentActiveItem = prevState.activeItem;

      if (
        currentActiveIndex !== null &&
        (currentActiveIndex >= data.length ||
          !data[currentActiveIndex] ||
          (currentActiveItem && data[currentActiveIndex].key !== currentActiveItem.key))
      ) {
        Object.assign(nextState, {
          activeIndex: null,
          activeItem: null,
        });
      }
    }

    if (activeIndex !== null && activeIndex !== undefined && activeIndex !== prevActiveIndex && data[activeIndex]) {
      Object.assign(nextState, {
        activeIndex,
        activeItem: data[activeIndex],
        needScrollToActive: true,
      });
    } else if (data !== prevData && restoreActiveIndex && activeItem && activeItem.key) {
      // Restore active index if there is an item with the same "key" property
      const index = data.findIndex(item => item.key === activeItem.key);
      if (index >= 0) {
        Object.assign(nextState, {
          activeIndex: index,
          activeItem: data[index],
        });
      }
    }

    return nextState;
  }