React-native-web: ScrollTo not working on react-native-web

Created on 18 May 2018  路  6Comments  路  Source: necolas/react-native-web

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.

Most helpful comment

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.

All 6 comments

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:

>         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?

If there's a problem make a new issue please

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.

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

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

buffaloDeveloper picture buffaloDeveloper  路  3Comments

EvanBacon picture EvanBacon  路  3Comments

ndbroadbent picture ndbroadbent  路  3Comments

bcpugh picture bcpugh  路  3Comments

rohanprabhu picture rohanprabhu  路  3Comments