Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 438
Louis345 Avatar asked Aug 06 '19 23:08

Louis345


People also ask

How do you scroll ScrollView programmatically in React Native?

You can assign a ref to your ScrollView. This ref contains a function called scrollTo() . Now all we need to do is scroll the ScrollView to the desired position.

How get position of ScrollView in React Native?

To get the current scroll position of ScrollView in React Native, we can set the onScroll prop of the ScrollView to a function that takes the scroll event object as an argument.

How do you scroll horizontally React Native?

In the ScrollView, we can scroll the components in both direction vertically and horizontally. By default, the ScrollView container scrolls its components and views in vertical. To scroll its components in horizontal, it uses the props horizontal: true (default, horizontal: false).


2 Answers

I was able to get this working by using a simple setTimeout of 1 second.

Here's what my code looks like now:

setTimeout(() => {
  this.scrollView.scrollTo({
    x: DEVICE_WIDTH * current_index,
    y: 0,
    animated: false
  });
}, 1)

Similar to what Micheal suggested above, it's likely that this is caused by a mounting issue due to the deprecated componentWillMount in React which wasn't properly updated in ScrollView.

like image 114
Monte Thakkar Avatar answered Oct 21 '22 02:10

Monte Thakkar


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.

like image 40
Louis345 Avatar answered Oct 21 '22 02:10

Louis345