如何解决错误?:node.scrollTo不是函数



当我在TodoItem组件中运行onPress时,我希望执行scrollTo并向下滚动y:高度值

但如果我在Press 上运行

node.scrollTo不是函数<lt;出现此错误

这是我的代码

(TodoList.js(

import React, {useContext, useState, useEffect, createRef} from 'react';
import {FlatList} from 'react-native';
import {
Dimensions,
NativeSyntheticEvent,
NativeScrollEvent,
ScrollView,
} from 'react-native';
const TodoList = ({replycomment}) => {

const height = Dimensions.get('window').height;
const [tabIndex, setTabIndex] = useState(0);
const flatListRef = React.useRef()
const refScrollView = createRef();

return (

<FlatList
ref={refScrollView}
style={{height}}
contentOffset={{x: 0, y: height}}
renderItem={({item}) => (
<TodoItem
onPress={() => {
const node = refScrollView.current;
if (node) {
node.scrollTo({x:0, y: height, animated: true});
}
}}
/>
)}
/>

(TodoItem.js(

import React, { useCallback, useState } from 'react';
import {FlatList} from 'react-native';

const TodoItem = ({onPress}) => {

return (


<MainContainer onPress={onPress}>
<Label>hi</Label>
</MainContainer>

我不知道为什么会发生这个错误。如何修复我的代码??我想使用平面列表

FlatListGithub源代码中,我只能看到4种有助于滚动功能的方法:-

scrollToEnd(params?: ?{animated?: ?boolean, ...})
scrollToIndex(params: {
animated?: ?boolean,
index: number,
viewOffset?: number,
viewPosition?: number,
...
})
scrollToItem(params: {
animated?: ?boolean,
item: ItemT,
viewPosition?: number,
...
})
scrollToOffset(params: {animated?: ?boolean, offset: number, ...}) 

我认为您需要利用上面的任何一个(因为FlatList没有实现它自己的scrollTo(。我可以在FlatList内部返回的VirtualizedList中看到scrollTo的使用情况。

链接到源代码-https://github.com/facebook/react-native/blob/master/Libraries/Lists/FlatList.js

你能尝试使用这样的东西吗:

node.scrollIntoView({behavior:"smooth",block:"start"}(

最新更新