react-native-card-stack-swiper icon indicating copy to clipboard operation
react-native-card-stack-swiper copied to clipboard

Add cards dynamically

Open liron-navon opened this issue 6 years ago • 14 comments

When I try to add more cards dynamically, the stack won't update, so I need to give a "key" to the Swiper and update the key, which forces rerender. I would rather add and remove children, but the Swiper won't notice such a change. I want to pull more cards data from an API (10-20 cards at a time) so it is important for me.

liron-navon avatar Mar 14 '19 14:03 liron-navon

Do you have a code fragment for this? Cant get my swiper to notice the change in state.

Schweig avatar May 28 '19 02:05 Schweig

@Schweig That's the basic code, I give it a key to force render it when I add or remove cards:

<Swiper key={cards.length}>

liron-navon avatar May 31 '19 20:05 liron-navon

@liron-navon did you find a solution? I'm facing the same issue.

shivamsaigupta avatar Jul 12 '19 12:07 shivamsaigupta

I will look into this next week

lhandel avatar Jul 12 '19 12:07 lhandel

Can you dont just append the card att the end of the list like this?

<CardStack onSwiped={this.addCard}>
{cards.map((card, index) => (
  <Card key={card} style={[styles.card]}>
    <Text style={styles.label}>{card}</Text>
  </Card>
))}
</CardStack>
addCard = () => {
    const { cards } = this.state;
    cards.push('New card')
    this.setState({ cards })
 }

lhandel avatar Oct 21 '19 09:10 lhandel

@lhandel It's been half a year or so since I opened this issue.... back then I think it just didn't work,

liron-navon avatar Oct 21 '19 14:10 liron-navon

I had this same issue. adding <CardStack key={cards.length} > worked for me too. The downside is that it then rerenders the whole stack so you're back to the first card again.

robsutcliffe avatar Oct 22 '19 20:10 robsutcliffe

any update?

safciplak avatar Mar 11 '20 13:03 safciplak

I am having this problema too.

viniciusmarson avatar May 14 '20 20:05 viniciusmarson

Can you dont just append the card att the end of the list like this?

<CardStack onSwiped={this.addCard}>
{cards.map((card, index) => (
  <Card key={card} style={[styles.card]}>
    <Text style={styles.label}>{card}</Text>
  </Card>
))}
</CardStack>
addCard = () => {
    const { cards } = this.state;
    cards.push('New card')
    this.setState({ cards })
 }

card When the initial value is empty,New data can't display if set <Swiper key={cards.length}> he downside is that it then rerenders the whole stack so you're back to the first card again.

CrazyMan2 avatar Jun 16 '20 10:06 CrazyMan2

hi guys, let's see if you can help me ... I add cards dynamically with firebase firestore and it seems to work fine but from time to time and randomly I get an error ... When the next batch of cards comes, the first card is rendered but the second card appears transparent and you can see the next smaller card from the back prepared by the animation. You can give the button like or dislike or swipe on that transparent card but the user does not see the content of that card and it is quite bad for the performance of the application. Has anyone something similar happened to you? Help please!

georgeMorales avatar Oct 15 '20 10:10 georgeMorales

hi guys, let's see if you can help me ... I add cards dynamically with firebase firestore and it seems to work fine but from time to time and randomly I get an error ... When the next batch of cards comes, the first card is rendered but the second card appears transparent and you can see the next smaller card from the back prepared by the animation. You can give the button like or dislike or swipe on that transparent card but the user does not see the content of that card and it is quite bad for the performance of the application. Has anyone something similar happened to you? Help please!

@georgeMorales Yep...same exact issue here, will update if I'm able to figure out a fix.

Snokown avatar Jul 18 '21 14:07 Snokown

@georgeMorales @Snokown How did you dynamically add to the stack without refreshing the whole stack back to the start? Thanks

luklew avatar Nov 04 '21 23:11 luklew

@georgeMorales @Snokown How did you dynamically add to the stack without refreshing the whole stack back to the start? Thanks

@luklew So, not the best solution but I got it working: I do refresh the whole stack back to the start and add the new cards onto it. I keep track of swipes by incrementing 'swipeCount' and then I offset the re-rendered stack by that variable in the code below. The card stack keeps growing this way, but for my app that didn't really matter.

cardData.slice(swipeCount).map((cardData) => ( <Card cardData={cardData} key={cardData.id} /> ))

Not ideal but hopefully it helps!

Snokown avatar Nov 05 '21 13:11 Snokown