react-native - scrollTo 在动画 ScrollView 上未定义

标签 react-native

使用 Animated.createAnimatedComponent(ScrollView) 时创建动画ScrollView无法再使用 scrollTo .

const AnimatedScrollView = Animated.createAnimatedComponent(ScrollView);

<AnimatedScrollView ref={(ref) => this.list = ref}>
  <View style={{height: 1000}} />
</AnimatedScrollView>

调用this.list.scrollTo({x: 0, y: 0})给出以下错误:
_this.list.scrollTo is not a function
它在普通的 ScrollView 上工作正常。有没有办法解决这个问题?

最佳答案

@max23_ 的答案现在可能有效,但不是正确的方法——根据经验,我们永远不应该直接访问私有(private)变量,因为这些变量经常会发生变化。 (编辑:没有不尊重:-))

由于this pull request ,获得包装组件的 ref 的新的和面向 future 的方法是使用getNode()实用程序方法,因为访问私有(private)变量(以 _ 开头)对于 future 的 API 更改是不安全的。

所以,新的做法是

ref={c => (this.myRef = c)}

然后在调用方法时,做
this.myRef.getNode().scrollTo({
  y: 0,
  animated: true,
});

:-)

关于react-native - scrollTo 在动画 ScrollView 上未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42051368/

相关文章:

javascript - 如何通过react中的包装类公开内部组件的函数?

ios - 模块 HMRClient 不是注册的可调用模块 (iOS)

reactjs - React Native 覆盖透明

css - React Native Flexbox 如何垂直对齐元素?

reactjs - Expo React Native App + Redux-Persist : AsyncStorage Problem

javascript - RXJS 5 bindCallback 当回调是第一个参数时

react-native - 无法在任何根 React Native Navigation 中找到入口文件 index.android.js

react-native - React Native textInput 最大长度

react-native 将 subview 放在前面

android - React Native,当用户在一段时间后恢复应用程序时刷新 Activity ?