import React, {useEffect, useRef, useState} from 'react'; import {FlatList, StyleSheet,…
- User
import React, {useEffect, useRef, useState} from 'react'; import {FlatList, StyleSheet, Text} from 'react-native'; import RNFS from 'react-native-fs'; import CardAyatList from '../../components/CardAyatList/CardAyatList.component'; import Loading from '../../components/loading/loading.component'; import {RootStackScreenProps} from '../../navigation/AppNavigator'; import {useAppSelector} from '../../store/hooks'; import {colors} from '../../themes/colors'; import AsyncStorage from '@react-native-async-storage/async-storage'; import {Separator} from '../../components/separator/separator.component';
const QuranDetailScreen = ({ route: { params: {id, data}, }, }: RootStackScreenProps<'QuranDetail'>) => { // --------------------------------------------------------------------------- // variables // --------------------------------------------------------------------------- const [isLoading, setIsLoading] = useState(false); const [currentPage, setCurrentPage] = useState(1); const flatListRef = useRef(null); const [scrollPosition, setScrollPosition] = useState(0); const {error, loading, refreshing} = useAppSelector(state => state.quranList);
// --------------------------------------------------------------------------- // useEffect // --------------------------------------------------------------------------- useEffect(() => { const getScrollPosition = async () => { const key =
scrollPosition_${data.id}; const position = await AsyncStorage.getItem(key); if (position !== null) { setScrollPosition(parseFloat(position)); } };codegetScrollPosition();}, [id]);
useEffect(() => { // Restore scroll position when component is mounted if (flatListRef.current && scrollPosition) { flatListRef.current.scrollToOffset({ offset: scrollPosition, animated: false, }); } }, [scrollPosition]);
useEffect(() => { const checkDiskSpace = async () => { try { const info = await RNFS.getFSInfo(); const {freeSpace} = info; console.log('Free space on the device:', freeSpace); } catch (error) { console.log('Error checking disk space:', error); } };
codecheckDiskSpace();}, []);
// --------------------------------------------------------------------------- // functions // --------------------------------------------------------------------------- const itemsPerPage = 10; const startIndex = (currentPage - 1) * itemsPerPage; const endIndex = startIndex + itemsPerPage;
// Save scroll position // const handleScroll = (event: any) => { // const yOffset = event.nativeEvent.contentOffset.y; // const key =
scrollPosition_${id}; // AsyncStorage.setItem(key, yOffset.toString()); // };const handleScroll = (event: any) => { const yOffset = event.nativeEvent.contentOffset.y; data.verses.forEach(async verse => { const key =
scrollPosition_${verse.id}; console.log('key1:', key); - Assistant
- User
I know my mistake that I'm saving data.id in here useEffect(() => { const getScrollPosition = async () => { const key =
scrollPosition_${data.id}; const position = await AsyncStorage.getItem(key); if (position !== null) { setScrollPosition(parseFloat(position)); } };codegetScrollPosition();}, [id]);, I should save data.verses.id, issues verses is array. How to achieve data.verses.id ?
- Assistant
- User
- Assistant