question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ScrollTo does not work on reanimated ScrollView

See original GitHub issue

When using scrollTo on a reanimated ScrollView ref, an error is thrown: “TypeError: node.scrollTo is not a function”. I have reproduced it in a snack:

https://snack.expo.io/@jacobarvidsson/2c7078

const App = () => {
  const scrollViewRef = useRef(null);
  useEffect(() => {
    if (scrollViewRef.current) {
      const node = scrollViewRef.current;
      if (node) {
        node.scrollTo({ x: 0, y: 0 });
      }
    }
  }, [])
    return (
      <View style={styles.container}>
        <Animated.ScrollView ref={scrollViewRef}>
          <Text>Test</Text>
        </Animated.ScrollView>
      </View>
    );
};

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

21reactions
emzet93commented, Nov 6, 2019

I’m facing the same issue. @jacobarvidsson did you find any workaround for this?

Edit: Actually the same problem occurs also when using default Animated API from react-native. The problem is that you get reference to the Animated component, not to the wrapped one. To access reference to ScrollView you can use getNode method. In your case it will look like this:

  useEffect(() => {
    if (scrollViewRef.current && scrollViewRef.current.getNode) {
      const node = scrollViewRef.current.getNode();
      if (node) {
        node.scrollTo({ x: 0, y: 0 });
      }
    }
  }, [])
4reactions
amanmanhascommented, Jun 1, 2020

Hi guys, you can solve it by using the getNode() method

There are two ways to solve this issue depending on how you are creating ref.-

  1. If you are using the new recommended way to create a ref to a component since React 16.3 is to use React.createRef() then you will use in this way:-
scrollView = React.createRef();
<Animated.ScrollView
  ref={this.scrollViewRef}/>
  this.scrollViewRef.current.getNode().scrollTo({x: number, y: number, animated: true|false})
  1. If you are using the previous way to create a ref before React 16.3 then you will use in this way:-
<Animated.ScrollView
  ref={c => (this.myRef = c)}/>
  this.myRef.getNode().scrollTo({
    x: number, 
    y: number,
    animated: true,
  });

The difference between the above two solution is , in one you can get the getNode() method inside the current and the other you can access it without current

Read more comments on GitHub >

github_iconTop Results From Across the Web

scrollTo | React Native Reanimated
Provides synchronous scroll on the UI thread to a given offset using an animated ref to a scroll view. This allows performing smooth...
Read more >
Attach ref to Reanimated ScrollView to access .scrollTo
It seems getNode is not available. I was able to to do ref.current._component.scrollTo it was weird. – Noitidart. Apr 9, 2021 ...
Read more >
Attach ref to Reanimated ScrollView to access .scrollTo- ...
React-native ScrollView scrollTo not working as it should · Detect scroll end event when calling scrollTo on ScrollView · How to access ref...
Read more >
reanimated-masonry-list
The FlatList won't offer you to draw MansonryList because when you provide numColumns bigger than 1 , the native view will switch to...
Read more >
Jelly Scroll with Reanimated 2 - YouTube
In this video, we are building the Jelly Scroll effect from the Cuberto agency with Reanimated v2 and v1.Wanna learn the fundamentals of ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found