How to re render cards in a swiper using react-native-deck-swiper

I’m facing an issue with updating the content of cards in a swiper using react-native-deck-swiper. I have a button external to the swiper, and when pressing it I want to update the content of the current card and trigger a re-render to reflect the changes.

I’ve managed to update the state of the item correctly, but the card doesn’t re-render immediately. It only renders again when I start swiping the item.

The documentation suggests a possible fix, stating, “A possible fix for the situation is setting the cardIndex on the parent component whenever deck re-renders are needed.” However, my attempt to implement this hasn’t been successful.

I have noticed that the change is render when the overlay condition are required (even if no overlay is set).

Below is a reproducible example of the problem :

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import React, { useRef, useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import Swiper from 'react-native-deck-swiper';
const cards = [
{ color: 'red', updated: false },
{ color: 'blue', updated: false },
{ color: 'green', updated: false },
{ color: 'yellow', updated: false },
{ color: 'purple', updated: false },
];
export default function App() {
const [cardIndex, setCardIndex] = useState(0);
const swiperRef = useRef(null);
const updateCard = () => {
cards[cardIndex].updated = !cards[cardIndex].updated;
// Force re-render??
setCardIndex(cardIndex);
};
const onSwiped = () => {
setCardIndex((cardIndex + 1) % cards.length);
};
const renderCard = (card) => (
<View style={[styles.card, { backgroundColor: card.color }]}>
{card.updated && <Text style={styles.updatedText}>UPDATED</Text>}
</View>
);
return (
<View style={styles.container}>
<Swiper
ref={swiperRef}
cards={cards}
renderCard={renderCard}
onSwiped={onSwiped}
onSwipedLeft={onSwiped}
onSwipedRight={onSwiped}
cardIndex={cardIndex}
infinite
/>
<TouchableOpacity style={styles.button} onPress={updateCard}>
<Text style={styles.buttonText}>Update card</Text>
</TouchableOpacity>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f7f7f7',
alignItems: 'center',
justifyContent: 'center',
},
card: {
width: '80%',
height: '80%',
justifyContent: 'center',
alignItems: 'center',
alignSelf: 'center',
},
updatedText: {
position: 'absolute',
fontSize: 24,
fontWeight: 'bold',
color: 'black',
},
button: {
position: 'absolute',
bottom: 20,
padding: 10,
backgroundColor: 'blue',
borderRadius: 5,
},
buttonText: {
color: 'white',
fontSize: 16,
},
});
</code>
<code>import React, { useRef, useState } from 'react'; import { View, Text, TouchableOpacity, StyleSheet } from 'react-native'; import Swiper from 'react-native-deck-swiper'; const cards = [ { color: 'red', updated: false }, { color: 'blue', updated: false }, { color: 'green', updated: false }, { color: 'yellow', updated: false }, { color: 'purple', updated: false }, ]; export default function App() { const [cardIndex, setCardIndex] = useState(0); const swiperRef = useRef(null); const updateCard = () => { cards[cardIndex].updated = !cards[cardIndex].updated; // Force re-render?? setCardIndex(cardIndex); }; const onSwiped = () => { setCardIndex((cardIndex + 1) % cards.length); }; const renderCard = (card) => ( <View style={[styles.card, { backgroundColor: card.color }]}> {card.updated && <Text style={styles.updatedText}>UPDATED</Text>} </View> ); return ( <View style={styles.container}> <Swiper ref={swiperRef} cards={cards} renderCard={renderCard} onSwiped={onSwiped} onSwipedLeft={onSwiped} onSwipedRight={onSwiped} cardIndex={cardIndex} infinite /> <TouchableOpacity style={styles.button} onPress={updateCard}> <Text style={styles.buttonText}>Update card</Text> </TouchableOpacity> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#f7f7f7', alignItems: 'center', justifyContent: 'center', }, card: { width: '80%', height: '80%', justifyContent: 'center', alignItems: 'center', alignSelf: 'center', }, updatedText: { position: 'absolute', fontSize: 24, fontWeight: 'bold', color: 'black', }, button: { position: 'absolute', bottom: 20, padding: 10, backgroundColor: 'blue', borderRadius: 5, }, buttonText: { color: 'white', fontSize: 16, }, }); </code>
import React, { useRef, useState } from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';
import Swiper from 'react-native-deck-swiper';

const cards = [
    { color: 'red', updated: false },
    { color: 'blue', updated: false },
    { color: 'green', updated: false },
    { color: 'yellow', updated: false },
    { color: 'purple', updated: false },
];

export default function App() {
    const [cardIndex, setCardIndex] = useState(0);
    const swiperRef = useRef(null);

    const updateCard = () => {
        cards[cardIndex].updated = !cards[cardIndex].updated;
        // Force re-render??
        setCardIndex(cardIndex); 
    };

    const onSwiped = () => {
        setCardIndex((cardIndex + 1) % cards.length); 
    };

    const renderCard = (card) => (
        <View style={[styles.card, { backgroundColor: card.color }]}>
            {card.updated && <Text style={styles.updatedText}>UPDATED</Text>}
        </View>
    );

    return (
        <View style={styles.container}>
            <Swiper
                ref={swiperRef}
                cards={cards}
                renderCard={renderCard}
                onSwiped={onSwiped}
                onSwipedLeft={onSwiped}
                onSwipedRight={onSwiped}
                cardIndex={cardIndex}
                infinite
            />
            <TouchableOpacity style={styles.button} onPress={updateCard}>
                <Text style={styles.buttonText}>Update card</Text>
            </TouchableOpacity>
        </View>
    );
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#f7f7f7',
        alignItems: 'center',
        justifyContent: 'center',
    },
    card: {
        width: '80%',
        height: '80%',
        justifyContent: 'center',
        alignItems: 'center',
        alignSelf: 'center',
      },
    updatedText: {
        position: 'absolute',
        fontSize: 24,
        fontWeight: 'bold',
        color: 'black',
    },
    button: {
        position: 'absolute',
        bottom: 20,
        padding: 10,
        backgroundColor: 'blue',
        borderRadius: 5,
    },
    buttonText: {
        color: 'white',
        fontSize: 16,
    },
});

I’d appreciate any insights on how to solve this issue and get the card to re-render immediately upon pressing the button. Thank you!

New contributor

Alexandre is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

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