React Native 当平面列表在平面列表或滚动视图中时,我得到了一个类型错误

68de4m5k  于 2023-03-03  发布在  React
关注(0)|答案(1)|浏览(133)
class TestClass extends PureComponent<Props> {
    private leftFilterFlatListRef = null;
    private rightFilterFlatListRef = null;

    private setLeftFilterFlatListRef = ref => {
        this.leftFilterFlatListRef = ref;
    };
    private setRightFilterFlatListRef = ref => {
        this.rightFilterFlatListRef = ref;
    };
    private onRightFilterGroupItemTitleClickInner = index => {
        this.rightFilterFlatListRef &&
            this.rightFilterFlatListRef.scrollToIndex({
                animated: true,
                index,
                viewPosition: 0
            });
    };
    private onLeftFilterItemClickInner = index => {
        console.log('======context====' + JSON.stringify(this.rightFilterFlatListRef.scrollToIndex)); // undefined
        this.rightFilterFlatListRef &&
            this.rightFilterFlatListRef.scrollToIndex({
                animated: true,
                index,
                viewPosition: 0
            });
    };
    public leftFilterScrollTo = index => {
        this.leftFilterFlatListRef &&
            this.leftFilterFlatListRef.scrollToIndex({
                animated: true,
                index: index,
                viewPosition: 1
            });
    };
    public rightFilterScrollTo = index => {
        this.rightFilterFlatListRef &&
            this.rightFilterFlatListRef.scrollToIndex({
                animated: true,
                index,
                viewPosition: 0
            });
    };

    private renderLeftFilterItem = () => {
        return <LeftItemComponent item={item} index={index} onLeftFilterItemClick={this.onLeftFilterItemClickInner} />;
    };

    private renderRightFilterItem = () => {
        return (
            <RightItemComponent
                index={index}
                item={item}
                onRightFilterGroupItemTitleClick={this.onRightFilterGroupItemTitleClickInner}
            />
        );
    };
    private renderLeftFlatItem = ({ item, index }) => {
        return this.renderLeftFilterItem(this.props, item, index, this.props.onVisible);
    };
    private renderRightFlatItem = ({ item, index }) => {
        return this.renderRightFilterItem(this.props, item, index, this.props.onVisible);
    };

    render(): React.ReactNode {
        const { leftFilterData, rightFilterData } = this.props;
        const nonNullLeftFilterData = leftFilterData || [];
        const nonNullRightFilterData = rightFilterData || [];
        return (
            <View>
                <FlatList
                    data={nonNullLeftFilterData}
                    ref={this.setLeftFilterFlatListRef}
                    renderItem={this.renderLeftFlatItem}
                />
                <FlatList
                    data={nonNullRightFilterData}
                    ref={this.setRightFilterFlatListRef}
                    renderItem={this.renderRightFlatItem}
                />
            </View>
        );
    }
}

我收到错误消息
类型错误:undefined不是函数(靠近...“this._scrollRef.scrollTo ...”)
当我把TestClass放到一个FlatList或者ScrollView里面的时候,我发现从FlatList得到的Ref.scrollToIndex是未定义的,但是我想得到scrollToIndex函数,并且应该正确的执行。

vxf3dgd4

vxf3dgd41#

谢谢大家。我理解为什么这个。rightFilterFlatListRef。scrollToIndex不能正确执行。VirtualizedList中有一些代码。

_defaultRenderScrollComponent = props => {
    const onRefresh = props.onRefresh;
    if (this._isNestedWithSameOrientation()) {
      // $FlowFixMe - Typing ReactNativeComponent revealed errors
      return <View {...props} />;
    } else if (onRefresh) {
      invariant(
        typeof props.refreshing === 'boolean',
        '`refreshing` prop must be set as a boolean in order to use `onRefresh`, but got `' +
          /* $FlowFixMe(>=0.111.0 site=react_native_fb) This comment suppresses
           * an error found when Flow v0.111 was deployed. To see the error,
           * delete this comment and run Flow. */
          JSON.stringify(props.refreshing) +
          '`',
      );
      return (
        // $FlowFixMe Invalid prop usage
        <ScrollView
          {...props}
          refreshControl={
            props.refreshControl == null ? (
              <RefreshControl
                refreshing={props.refreshing}
                onRefresh={onRefresh}
                progressViewOffset={props.progressViewOffset}
              />
            ) : (
              props.refreshControl
            )
          }
        />
      );
    } else {
      // $FlowFixMe Invalid prop usage
      return <ScrollView {...props} />;
    }
  };

当平面列表A位于平面列表B内部时,平面列表A变为视图
我们添加〈ScrollView {... props} /〉作为平面列表A的一个属性。然后平面列表A可以再次滚动。

相关问题