【发布时间】:2021-03-16 18:16:56
【问题描述】:
当我在 TodoItem 组件中运行 onPress 时,我希望执行 scrollTo 并向下滚动 y: height value
但如果我运行 onPress
node.scrollTo 不是函数
这是我的代码
(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>
我不确定为什么会发生此错误。我该如何修复我的代码?我想使用平面列表
【问题讨论】:
-
node中存在的对象显然没有scrollTo方法。
标签: javascript node.js reactjs react-native