react-native-web icon indicating copy to clipboard operation
react-native-web copied to clipboard

ScrollTo not working on react-native-web

Open sumanth234 opened this issue 8 years ago • 8 comments

I want to scroll to end on load of react-native-web page.But ScrollTo and scrollToEnd function does not seem to be working.

Could you please check this and revert back to me.

sumanth234 avatar May 18 '18 06:05 sumanth234

Can you provide an example that doesn't work?

Storybook works http://necolas.github.io/react-native-web/storybook/?selectedKind=Components&selectedStory=ScrollView&full=0&addons=0&stories=1&panelRight=0

necolas avatar May 18 '18 14:05 necolas

I stumbled upon this issue. scrollTo works when the the ScrollView is vertical, but it doesn't work (or maybe I don't know how to make it work) when the "horizontal" prop is set.

Diff from original storybook demo:

14a15
>         horizontal
34c35
<             this.scrollview.scrollTo({ y: 100 });
---
>             this.scrollview.scrollTo({ x: 100 });

Repro Complete Code:

/* eslint-disable react/jsx-no-bind */

/**
 * @flow
 */

import React, { PureComponent } from 'react';
import { Button, ScrollView, StyleSheet, Text, TouchableHighlight, View } from 'react-native';

export default class ScrollToExample extends PureComponent {
  render() {
    return (
      <View style={styles.scrollViewContainer}>
        <ScrollView
        horizontal
          contentContainerStyle={styles.scrollViewContentContainerStyle}
          ref={scrollview => {
            this.scrollview = scrollview;
          }}
          scrollEventThrottle={16} // ~60 events per second
          style={styles.scrollViewStyle}
        >
          {Array.from({ length: 50 }).map((item, i) => (
            <TouchableHighlight
              key={i}
              onPress={() => {}}
              style={[styles.box, styles.horizontalBox]}
            >
              <Text>{i}</Text>
            </TouchableHighlight>
          ))}
        </ScrollView>
        <Button
          onPress={() => {
            this.scrollview.scrollTo({ x: 100 });
          }}
          title="Scroll to 100px"
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  box: {
    flexGrow: 1,
    justifyContent: 'center',
    borderWidth: 1
  },
  scrollViewContainer: {
    height: 150,
    width: 300
  },
  scrollViewStyle: {
    borderWidth: 1,
    marginBottom: '1.3125rem'
  },
  scrollViewContentContainerStyle: {
    backgroundColor: '#eee',
    padding: 10
  }
});

I am trying to implement draggable scrolling. Are there any workarounds for this?

murtraja avatar Nov 13 '18 10:11 murtraja

If there's a problem make a new issue please

necolas avatar Nov 13 '18 15:11 necolas

Just a note to say that it might also be because you don't constrain the height of the container of the ScrollView. And thus the container expands as much as it needs, and scroll happens on the <body> element instead of on the ScrollView DOM element.

eightyfive avatar Dec 20 '18 18:12 eightyfive

Thanks alot @eightyfive been stuck with this problem but i tried what you told and it worked :)

vinaysandesh avatar Jan 02 '21 06:01 vinaysandesh

If anyone else is still stuck on this with a horizontal scrollview try setting the width of your scrollview component to a valid value. This is what cased my issues on web

jackdewhurst avatar Feb 15 '21 15:02 jackdewhurst

Hi, on horizontal scrollview not working, someone can help me?

condorman avatar Oct 07 '21 10:10 condorman

Hi, on horizontal scrollview not working, someone can help me?

Try: yourScrollViewRef.current.getNode().scrollToIndex({index: yourIndex})

use getNode() instead of scrollTo directly

haddad-yacine avatar Oct 11 '21 23:10 haddad-yacine