代码之家  ›  专栏  ›  技术社区  ›  user15322469

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

  •  0
  • user15322469  · 技术社区  · 4 年前

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

    但如果我在Press上跑步

    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>
    

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

    0 回复  |  直到 4 年前
        1
  •  1
  •   Lakshya Thakur    4 年前

    github 源代码 FlatList ,我只能看到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, ...}) 
    

    我认为你需要利用上述任何一种(因为 平面列表 不实现自己的 scrollTo ). 我能看见 滚动到 内部使用 VirtualizedList 由内部返回 平面列表 .

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

        2
  •  0
  •   Ankur Munjaal    4 年前

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

    node.scrullIntoView({行为:“平滑”,块:“开始”})