Skip to content

React native flatlist item shifting right #32059

@volkandogan

Description

@volkandogan

I want to show 3 items on screen and scroolling using animation. first 10 item show up normally but numbers start to shift to the right as scroll forward. I think it is a bug of flatlist.

 import React, {useState, useEffect} from 'react';
 import {
 StyleSheet,
 Text,
 View,
 FlatList,
 Platform,
 Dimensions,
 Animated,
 } from 'react-native';
const {width, height} = Dimensions.get('window');
const ITEM_SIZE =
  Platform.OS === 'ios' ? width * 0.33 - 32 : (width - 32) * 
   0.33;
  const ITEM_SPACING = ITEM_SIZE;

     const data = [
    {key: '1'},
    {key: '2'},
    {key: '3'},
    {key: '4'},
    {key: '5'},
    {key: '6'},
    {key: '7'},
    {key: '8'},
    {key: '9'},
    {key: '10'},
    {key: '11'},
    {key: '12'},
    {key: '13'},
    {key: '14'},
    {key: '15'},
    {key: '16'},
    {key: '17'},
    {key: '18'},
    {key: '19'},
    {key: '20'},
    {key: '21'},
    {key: '22'},
    {key: '23'},
    {key: '24'},
    {key: '25'},
    {key: '26'},
    {key: '27'},
    {key: '28'},
    {key: '29'},
    {key: '30'},
    {key: '31'},
    {key: '32'},
  ];
 const Onboarding = () => {
 const [selectedId, setSelectedId] = useState('23');
 const scrollX = React.useRef(new Animated.Value(0)).current;
return (
<View
  style={{
    flex: 1,
    marginHorizontal: 16,
    justifyContent: 'center',
    alignItems: 'center',
  }}>
  <FlatList
    showsHorizontalScrollIndicator={false}
    data={data}
    horizontal
    snapToInterval={ITEM_SIZE}
    keyExtractor={item => item.key}
    extraData={selectedId}
    decelerationRate={0}
    style={{flexGrow: 0}}
    bounces={false}
    onMomentumScrollEnd={ev => {
      const index = Math.round(ev.nativeEvent.contentOffset.x / ITEM_SIZE);
      setSelectedId(data[index].key);
    }}
    onScroll={Animated.event(
      [{nativeEvent: {contentOffset: {x: scrollX}}}],
      {useNativeDriver: false},
    )}
    scrollEventThrottle={16}
    contentContainerStyle={{
      justifyContent: 'center',
      alignItems: 'center',
      paddingHorizontal: ITEM_SPACING,
    }}
    getItemLayout={(item, index) => ({
      length: ITEM_SIZE,
      offset: ITEM_SIZE * index,
      index,
    })}
    renderItem={({item, index}) => {
      const inputRange = [
        (index - 1) * ITEM_SIZE,
        index * ITEM_SIZE,
        (index + 1) * ITEM_SIZE,
      ];
      const opacity = scrollX.interpolate({
        inputRange,
        outputRange: [0.2, 1, 0.2],
      });
      return (
        <>
          <View style={[styles.item]}>
            <View
              style={{
                justifyContent: 'center',
                alignItems: 'center',
              }}>
              <Animated.Text style={[styles.text, {opacity}]}>
                {item.key}
              </Animated.Text>
            </View>
          </View>
        </>
      );x
      width: 60,
      height: 60,
      borderRadius: 30,
      position: 'absolute',
      borderWidth: 10,
      borderColor: '#000',
    }}></View>
</View>
 );
};

 export default Onboarding;

 const styles = StyleSheet.create({
  container: {
   paddingTop: 22,
   },
 item: {
  fontSize: 13,
   width: ITEM_SIZE,
   height: 55,
   justifyContent: 'center',
    alignItems: 'center',
 },
 flatList: {
   justifyContent: 'center',
     alignItems: 'center',
  },
  text: {
   fontSize: 16,
  },
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Triage 🔍StaleThere has been a lack of activity on this issue and it may be closed soon.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions