Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView RTL in react native

The following code is my ScrollView in a react native project:

  <ScrollView
    ref={(scrollView) => { this._scrollView = scrollView; }}
    horizontal={true}
    showsHorizontalScrollIndicator={false}
    showsVerticalScrollIndicator={false}
    directionalLockEnabled={true}
    bounces={false}
    scrollsToTop={false}
  >

Now it moves from left to right, How it could move from right to left in first loading?

like image 707
gregory Avatar asked Dec 08 '16 09:12

gregory


People also ask

How do you reverse a ScrollView in react native?

set flexDirection: 'row-reverse' to ScrollView's style, which will order your items from right to left. use onContentSizeChange to init list's scroll on the right side.

How use ScrollView horizontal in 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).

Can scroll in ScrollView react native?

The ScrollView is a generic scrolling container that can contain multiple components and views. The scrollable items can be heterogeneous, and you can scroll both vertically and horizontally (by setting the horizontal property).


2 Answers

For RTL setting you should write your code like the below sample:

import React, { useRef } from 'react';
import { ScrollView, StyleSheet } from 'react-native';

const RTLScrollView = () => {
  const scrollRef = useRef();
  const scrollToEnd = () => scrollRef.current.scrollToEnd({ animated: false });

  return (
    <ScrollView
      horizontal
      ref={scrollRef}
      showsHorizontalScrollIndicator={false}
      onContentSizeChange={scrollToEnd}
      contentContainerStyle={styles.contentContainerStyle}
    >
      ~~~
      ~~~
      ~~~
    </ScrollView>
  );
}

const styles = StyleSheet.create({
  contentContainerStyle: {
    flexDirection: 'row-reverse'
  }
});

export default RTLScrollView;

Hint: I don't use your other ScrollView settings like bounces={false}, If you want, put it in your code, my answer is just a sample.

like image 172
AmerllicA Avatar answered Sep 28 '22 12:09

AmerllicA


This is truly an annoying Bug in React Native , ScrollView+RTL=Silly Bug.

Though , there are multiple hacks you can adapt , I did this to overcome the bug :

  1. I reversed the data array I am using.
  2. Used : onContentSizeChange event handler to trigger the scrollToEnd({ animated: false }) function on ScrollView
like image 20
Ali Sao Avatar answered Sep 28 '22 12:09

Ali Sao