【问题标题】:KeyboardAwareScrollView props innerRef scrollToEnd not workingKeyboardAwareScrollView 道具 innerRef scrollToEnd 不起作用
【发布时间】:2022-07-27 21:54:14
【问题描述】:

我正在尝试在使用 https://github.com/APSL/react-native-keyboard-aware-scroll-view 的屏幕中调用 scrollToEnd(),但出现错误:

“无法读取未定义的属性'props'”。

我的代码如下所示:

让滚动

(在文件的开头)

然后,在返回内部:

<KeyboardAwareScrollView innerRef={ref => { scroll = ref }}>
my scrollable code
</KeyboardAwareScrollView>

然后有一个按钮

onPress={() => scroll.props.scrolltoEnd()}

单击按钮会出现上述错误,这让我认为我没有正确使用 innerRef?我是否需要在某些时候使用 useRef ?任何帮助表示赞赏,谢谢!

【问题讨论】:

    标签: react-native api npm


    【解决方案1】:

    在 KeyboardAwareScrollView 上使用 React.useRef

     const scrollRef = React.useRef(null);
    
    
     <KeyboardAwareScrollView
          ref={scrollRef}
          ....
    

    根据您的输入,聆听 onContentSizeChange

     <Input
    
     onContentSizeChange={(e) => {
                    if (scrollRef && scrollRef.current) {
                      scrollRef.current?.scrollToEnd();
                    }
                  }}
     />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2021-10-29
      • 2022-10-21
      相关资源
      最近更新 更多