webraptor / react-native-deck-swiper

tinder like react-native deck swiper
ISC License
126 stars 81 forks source link

Reloading Swiper Card List #69

Open Nestor231 opened 2 years ago

Nestor231 commented 2 years ago

Hello @webraptor,

Id like to ask if there's a way we can reload the swiper with a new list of cards ? I tried Setting state in the component like this: const[cardState, setCardState] = useState({cards: ['item1','item2','item3'], allSwipe: false})

then set the swiper's card list like this:

<Swiper cards={cardState.cards} ...{other.props} />

then had a reloadButton of sorts to refresh the list:

<Button onClick={()=>{ setCardState({cards:['item4','item5']}) }}

then i swiped all cards until they reach zero, and clicked my reload Button. But nothing happened. Need your guidance on this

SDE-Arvind commented 2 years ago

you can try (This is working in my project) const[cardState, setCardState] = useState( ['item1','item2','item3']) <Swiper cards={cards} ...{other.props} />

<Button onClick={()=>{ setCardState(['item4','item5']) }}

RuzenDev commented 1 year ago

No, just changing the state is not rerendering

@webraptor Any suggestions plz

forchello commented 8 months ago

Hi all, I've encountered the problem of the impossibility of updating the array of cards. Dynamic changing of the card array state does not help. Then the following solution came to mind:

Create a key state that changes with each swipe:

const [key, setKey] = useState(0);

<Swiper key={key} cards={cards} onSwiped={() => setKey(key+1)} {...otherProps} />

Thus, each time the key property changes, the Swiper component will be rerender and the array of items will change accordingly.

paolopedrigal commented 2 months ago

this solved this issue for me

Hi all, I've encountered the problem of the impossibility of updating the array of cards. Dynamic changing of the card array state does not help. Then the following solution came to mind:

Create a key state that changes with each swipe:

const [key, setKey] = useState(0);

<Swiper key={key} cards={cards} onSwiped={() => setKey(key+1)} {...otherProps} />

Thus, each time the key property changes, the Swiper component will be rerender and the array of items will change accordingly.