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
函数,并且应该正确的执行。
1条答案
按热度按时间vxf3dgd41#
谢谢大家。我理解为什么这个。rightFilterFlatListRef。scrollToIndex不能正确执行。VirtualizedList中有一些代码。
当平面列表A位于平面列表B内部时,平面列表A变为视图
我们添加〈ScrollView {... props} /〉作为平面列表A的一个属性。然后平面列表A可以再次滚动。