scrollTo is undefined on animated ScrollView

前端 未结 5 1858
不知归路
不知归路 2021-02-02 08:03

When using Animated.createAnimatedComponent(ScrollView) to create an animated ScrollView it\'s not longer possible to use scrollTo.

<
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-02 08:40

    Building on the answer from @jhm - the new, recommended way to create a ref to a component since React 16.3 is to use React.createRef() as mentioned by @cyqui.

    With a normal (note: non-animated) component, we'd simple create a ref to the ScrollView in the recommended way:

    scrollView = React.createRef();
    
    
    

    and use static methods as such:

    this.scrollView.current.scrollTo({x: number, y: number, animated: true|false})
    

    EDIT: As of RN 0.62 the below is no longer necessary

    However, when working with an Animated component, we're moving into direct manipulation which requires us to get the native node of the component before calling any static methods. Therefore, you'd end up with the following:

    scrollView = React.createRef();
    
    
    
    this.scrollView.current.getNode().scrollTo({x: number, y: number, animated: true|false})
    

提交回复
热议问题