reactjs - React Hooks - 带有钩子的功能组件中 ScrollView 的引用?
问题描述
我将一个反应原生项目从所有类组件转换为带有钩子的功能组件。我有一个带有自动滚动到底部的 ScrollView 的消息传递页面。我如何将其转换为在功能组件中工作?使用 ref 和 this.scrollView 会导致错误。
<ScrollView
ref={ref => this.scrollView = ref}
onContentSizeChange={(contentWidth, contentHeight)=> {this.scrollView.scrollToEnd({animated: true})}}
>
{...content...}
</ScrollView>
解决方案
在组件的主体中:
function App(props) {
const scrollViewRef = useRef();
...
在组件上:
<ScrollView
ref={scrollViewRef}
onContentSizeChange={(contentWidth, contentHeight)=> {scrollViewRef.current.scrollToEnd({animated: true})}}
>
{...content...}
</ScrollView>
推荐阅读
- python - 如何将嵌套字典转换为 pandas DataFrame
- c# - rdp 文件启动调试而不是在服务器上
- xslt - 如何在 XSLT 中为 if 条件创建布尔变量?
- python - 如何在 Turtle Racer 中添加 1st 2nd 3rd 功能?
- spring-boot - Spring Boot 应用程序 - 如何以 Json 格式打印日志
- c# - 使用身份服务器时如何注销?
- jsp - 在登录、注销、索引页面之间导航时,由于 Servlet 在浏览器的搜索栏中显示错误的 URL
- javascript - 条件匹配时,对象数组返回对象
- rest - 通过 REST API 使用 ml-engine 批量预测
- java - 使用 Java 仅更新现有 Google Drive 文件的元数据