How do i handle interpolated Animated and dot indicator gracefully while I have a copy of data list?

I want to create a bidirectional swiper my own, but I have a problem about to seek a way how do i handle the interpolated. here’s my swiper so far

Swiper.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import React, {useCallback, useRef} from 'react';
import {Animated, View} from 'react-native';
const Swiper = ({
RenderItem = () => null,
data = [],
RenderDotIndicator = false,
containerWidth = 0,
}) => {
const refAnimated = useRef(new Animated.Value(0)).current;
// here I copy the data that I got from props
// so if the data is [1,2] it would be [1,2,1,2]
const [InternalData, setInternalData] = React.useState([...data, ...data]);
// here I want to handle that interpolated only used 2 length instead of 4
const interpolateFunct = (outputRange, index) =>
refAnimated.interpolate({
inputRange: [
(index - 1) * width,
index * width,
(index + 1) * width,
],
outputRange,
extrapolate: 'clamp',
});
const WrappedRenderItem = useCallback(
({item, index}) => (
<View style={{overflow: 'hidden', width: width}}>
{RenderItem({item, index})}
</View>
),
[],
);
const WrappedRenderDotIndicator = useCallback(() => {
const dotItem = data.map((item, index) => {
return (
<Animated.View
key={'dot' + item + index.toString()}
style={{
width: 6,
backgroundColor: '#D3D3D3',
height: 6,
borderRadius: 6 / 2,
marginHorizontal: 5,
transform: [{scale: interpolateFunct([1, 2, 1], index)}],
}}
/>
);
});
return (
<View
style={{
flexDirection: 'row',
alignSelf: 'center',
position: 'absolute',
bottom: 6,
}}
>
{dotItem}
</View>
);
}, [RenderDotIndicator]);
return (
<View>
<Animated.FlatList
ref={refAnimated}
data={InternalData}
renderItem={WrappedRenderItem}
horizontal
pagingEnabled
onScroll={Animated.event(
[{nativeEvent: {contentOffset: {x: refAnimated}}}],
{useNativeDriver: true},
)}
/>
<WrappedRenderDotIndicator />
</View>
);
};
Swiper.displayName = 'Swiper';
export default Swiper;
</code>
<code>import React, {useCallback, useRef} from 'react'; import {Animated, View} from 'react-native'; const Swiper = ({ RenderItem = () => null, data = [], RenderDotIndicator = false, containerWidth = 0, }) => { const refAnimated = useRef(new Animated.Value(0)).current; // here I copy the data that I got from props // so if the data is [1,2] it would be [1,2,1,2] const [InternalData, setInternalData] = React.useState([...data, ...data]); // here I want to handle that interpolated only used 2 length instead of 4 const interpolateFunct = (outputRange, index) => refAnimated.interpolate({ inputRange: [ (index - 1) * width, index * width, (index + 1) * width, ], outputRange, extrapolate: 'clamp', }); const WrappedRenderItem = useCallback( ({item, index}) => ( <View style={{overflow: 'hidden', width: width}}> {RenderItem({item, index})} </View> ), [], ); const WrappedRenderDotIndicator = useCallback(() => { const dotItem = data.map((item, index) => { return ( <Animated.View key={'dot' + item + index.toString()} style={{ width: 6, backgroundColor: '#D3D3D3', height: 6, borderRadius: 6 / 2, marginHorizontal: 5, transform: [{scale: interpolateFunct([1, 2, 1], index)}], }} /> ); }); return ( <View style={{ flexDirection: 'row', alignSelf: 'center', position: 'absolute', bottom: 6, }} > {dotItem} </View> ); }, [RenderDotIndicator]); return ( <View> <Animated.FlatList ref={refAnimated} data={InternalData} renderItem={WrappedRenderItem} horizontal pagingEnabled onScroll={Animated.event( [{nativeEvent: {contentOffset: {x: refAnimated}}}], {useNativeDriver: true}, )} /> <WrappedRenderDotIndicator /> </View> ); }; Swiper.displayName = 'Swiper'; export default Swiper; </code>
import React, {useCallback, useRef} from 'react';
import {Animated, View} from 'react-native';

const Swiper = ({
  RenderItem = () => null,
  data = [],
  RenderDotIndicator = false,
  containerWidth = 0,
}) => {
  const refAnimated = useRef(new Animated.Value(0)).current;
  // here I copy the data that I got from props
  // so if the data is [1,2] it would be [1,2,1,2]
  const [InternalData, setInternalData] = React.useState([...data, ...data]);

  // here I want to handle that interpolated only used 2 length instead of 4
  const interpolateFunct = (outputRange, index) =>
    refAnimated.interpolate({
      inputRange: [
        (index - 1) * width,
        index * width,
        (index + 1) * width,
      ],
      outputRange,
      extrapolate: 'clamp',
    });

  const WrappedRenderItem = useCallback(
    ({item, index}) => (
      <View style={{overflow: 'hidden', width: width}}>
        {RenderItem({item, index})}
      </View>
    ),
    [],
  );

  const WrappedRenderDotIndicator = useCallback(() => {
    const dotItem = data.map((item, index) => {
      return (
        <Animated.View
          key={'dot' + item + index.toString()}
          style={{
            width: 6,
            backgroundColor: '#D3D3D3',
            height: 6,
            borderRadius: 6 / 2,
            marginHorizontal: 5,
            transform: [{scale: interpolateFunct([1, 2, 1], index)}],
          }}
        />
      );
    });
    return (
      <View
        style={{
          flexDirection: 'row',
          alignSelf: 'center',
          position: 'absolute',
          bottom: 6,
        }}
      >
        {dotItem}
      </View>
    );
  }, [RenderDotIndicator]);

  return (
    <View>
      <Animated.FlatList
        ref={refAnimated}
        data={InternalData}
        renderItem={WrappedRenderItem}
        horizontal
        pagingEnabled
        onScroll={Animated.event(
          [{nativeEvent: {contentOffset: {x: refAnimated}}}],
          {useNativeDriver: true},
        )}
      />
      <WrappedRenderDotIndicator />
    </View>
  );
};

Swiper.displayName = 'Swiper';

export default Swiper;

currently, using the code above, I got this behaviour (notice the dot indicator when I sliding from 2nd to 3rd image, the dot is not reflecting the first one)

https://jmp.sh/JeFQ2CbF (or run snippet below)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="https://jumpshare.com/embed/IFu1Zeev0hccyrbvqm9h" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div></code>
<code><div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="https://jumpshare.com/embed/IFu1Zeev0hccyrbvqm9h" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div></code>
<div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="https://jumpshare.com/embed/IFu1Zeev0hccyrbvqm9h" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>

How do I handle when sliding from 2nd to 3rd, the dot indicator reflecting the first one?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật