Sea*_*sey 2 reactjs react-native
我将 React Native 项目从所有类组件转换为带有钩子的功能组件。我有一个带有 ScrollView 的消息页面,可以自动滚动到底部。我如何将其转换为在功能组件中工作?使用 ref 和 this.scrollView 会导致错误。
<ScrollView
ref={ref => this.scrollView = ref}
onContentSizeChange={(contentWidth, contentHeight)=> {this.scrollView.scrollToEnd({animated: true})}}
>
{...content...}
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
Sam*_*l G 17
在组件的主体中:
function App(props) {
const scrollViewRef = useRef();
...
Run Code Online (Sandbox Code Playgroud)
在组件上:
<ScrollView
ref={scrollViewRef}
onContentSizeChange={(contentWidth, contentHeight)=> {scrollViewRef.current.scrollToEnd({animated: true})}}
>
{...content...}
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6866 次 |
| 最近记录: |