Search code examples
react-nativescrollview

React Native's Animated.ScrollView is not allowing me to programmatically scroll to a certain position


Prior to upgrading to the newest version of React-Native and Expo this code block was working. The version it was working on was "expo": "^32.0.0".

I was previously able to programmatically move a child of Animated.ScrollView and now I'm no longer able to do so. Here is my testing code block that I am testing.

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  Animated,
  TouchableOpacity,
  SafeAreaView,
  ScrollView
} from 'react-native';

export default class App extends React.Component {
  constructor(props) {
    super(props);
  }
  handleMove = () => {
    this.scroller.getNode().scrollTo({
      x: 200,
      y: 0,
      animated: false
    });
  };
  render() {
    return (
      <SafeAreaView style={styles.container}>
        <Text>Open up App.js to start working on your app!</Text>
        <Animated.ScrollView
          horizontal
          ref={scroller => {
            this.scroller = scroller;
          }}
        >
          <View
            style={{
              height: 100,
              width: 100,
              backgroundColor: 'red',
              margin: 5
            }}
          />
        </Animated.ScrollView>
        <TouchableOpacity onPress={this.handleMove}>
          <Text>Move</Text>
        </TouchableOpacity>
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  container: {}
});

After upgrading the code block no longer works on the newest version. The current version I am testing on is:

"dependencies": {
    "expo": "^34.0.1",
    "react": "16.8.3",
    "react-dom": "^16.8.6",
    "react-native": "https://github.com/expo/react-native/archive/sdk-34.0.0.tar.gz",
    "react-native-web": "^0.11.4"
  },

What is the correct way of doing this?

I added a snack to help illustrate my problem. ' https://snack.expo.io/@louis345/brave-banana


Solution

  • I was able to resolve my issue by adding a prop to the scrollView. scrollToOverflowEnabled={true}

    Now everything works as it once was. I hope this helps someone in the future.