I want to implement a feature that changes the Valaszok style based on if it's selected…
- User
I want to implement a feature that changes the Valaszok style based on if it's selected or not and I want it to save the style so when I horizontally scroll through the flatlist, when I scroll back to it, it would still show it as selected and when I press EllenorzesButton it would behave the same. My problem is the style is not saved with my current implementation, how can I fix it? Please write down the whole code with comments where it changes something
import React, { useCallback, useEffect, useRef, useState} from 'react'; import { Dimensions, FlatList, ScrollView, StyleSheet, Text, TouchableOpacity, View } from "react-native"; import Feladatok from "./Feladatok"; import { fetchAllData } from "../Database/Db"; import { Feladat } from '../Database/Feladat'; import { SafeAreaView } from 'react-native-safe-area-context'; import styles from './Teszt.style'; import { AntDesign as Icon } from '@expo/vector-icons';
const Darab = () => { const [feladatok, setFeladatok] = useState<Feladat[]>([]); const [ellenorizve, setEllenorizve] = useState(false); const [answeredQuestions, setAnsweredQuestions] = useState<number[]>([]); const [index, setIndex] = useState(0); const { width: windowWidth } = Dimensions.get("window"); const indexRef = useRef(index); indexRef.current = index;
useEffect(() => { handleFetchFeladatok(); }, []);
const handleFetchFeladatok = () => { fetchAllData() .then((result) => { setFeladatok(result); }) .catch((err) => { console.error("Error fetching feladatok: ", err); }); };
const ellenorzes = () => { setEllenorizve(true); setAnsweredQuestions((prevIndices) => [...prevIndices, index]); };
const renderItem = ({ item, index }: { item: Feladat; index: number }) => { const isAnswered = answeredQuestions.includes(index); return ; }
const onScroll = useCallback((event: any) => { const slideSize = event.nativeEvent.layoutMeasurement.width; const index = event.nativeEvent.contentOffset.x / slideSize; const roundIndex = Math.round(index);
const distance = Math.abs(roundIndex - index); const isNoMansLand = 0.4 < distance; if (roundIndex !== indexRef.current && !isNoMansLand) { setIndex(roundIndex); const currentPageAnswered = answeredQuestions.includes(roundIndex); setEllenorizve(currentPageAnswered); }}, [answeredQuestions]);
const flatListOptimizationProps = { pagingEnabled: true, horizontal: true, initialNumToRender: 0, maxToRenderPerBatch: 3, scrollEventThrottle: 16, windowSize: 8, getItemLayout: useCallback( (_: any, index: number) => ({ index, length: windowWidth, offset: index * windowWidth, }), [windowWidth] ), };
return ( <FlatList keyExtractor={(item) => String(item.ID)} overScrollMode="never" data={feladatok} renderItem={renderItem} showsHorizontalScrollIndicator={false} onScroll={onScroll} {...flatListOptimizationProps} /> <View style={[styles.navBtnContainer, styles.prev]}> <TouchableOpacity style={!ellenorizve ? [styles.ellenorzesBtn, styles.activeBtn] : [styles.ellenorzesBtn, styles.inActiveBtn]} onPress={ellenorzes} disabled={ellenorizve} > <Text style={!ellenorizve ? [styles.ellenorzesBtnText, styles.activeText] : [styles.ellenorzesBtnText, styles.inActiveText]} > Ellenőrzés ); };
- Assistant
- User
- Assistant