Open Cantum2 opened 3 years ago
@Cantum2 thanks for the detailed report! There's a couple of open PRs which I hope I'll get the time to review & merge this week. I don't think any of them fixes this issue though
No problem! I know the struggle when people arent thorough.
Is there a temporary work around I could implement or something of the sort for this to work? This is a crucial feature for my application.
For us what worked was to keep the cardIndex outside of the app, so whenever we pushed new cards to the deck that would get updated and the deck would re-render
Are you suggesting creating a wrapper for my swiper functional component that contains the index. Then I pass the index from the wrapper into the swiper component?
I'm a bit new to react native so I hope I'm making sense.
Are you suggesting creating a wrapper for my swiper functional component that contains the index. Then I pass the index from the wrapper into the swiper component?
I'm a bit new to react native so I hope I'm making sense.
Yes, I think the workaround was mentioned before in at least one of the related issues you've mentioned.
I had seen that solution be suggested in there but misunderstood it. I will give this a try and let you know how it works and if it does I'll post the code because I've seen many people run into this same issue.
Thank you for being so prompt!
I moved the index to the parent and pass it into the child and still no luck, the renderCard function in the Swiper does not get triggered thus resulting in the new items not rendering. I am still stuck on this, the behavior is exactly the same. Please let me know if you see something wrong here.
...
const getProductsByTag = () => {
console.log('fetching products', tags);
setLoading(true);
axios.get('/products/list', {
params: { tags: tags.join(','), page, size: 5 }
}).then((res) => {
const { products: internalProds, count } = res.data;
const productResponse: IProduct[] = internalProds;
console.log(count);
setLoading(false);
setTotalSize(count);
setProducts(products.concat(productResponse));
}).catch((err) => {
setError('Error getting products');
})
}
...
const userLikeProduct = (product: IProduct) => {
setIndex((index + 1) % products.length);
transitionRef.current.animateNextTransition();
const likeActions = [
// calls removed for brevity
]
Promise.all(likeActions).then((res) => {
if (products.length - 2 <= index) {
console.log('getting more products in like product')
setPage(page++)
getProductsByTag();
}
}).catch((err) => {
console.log("Error in user like product")
})
}
...
<View style={{ height: 25 }} />
<View style={SwipeStyles.swiperContainer}>
<SimplySwiper index={index} onUserLike={userLikeProduct} onUserDislike={userDislikeProduct} products={products}/>
</View>
...
...
interface props {
index: number;
onUserLike: (product: IProduct) => void;
onUserDislike: (product: IProduct) => void;
products: IProduct[];
}
...
const renderCard = (tProduct: IProduct, tIndex: number) => {
console.log('Render card', tProduct._id, tIndex)
return <ProductCard key={tProduct._id} index={tIndex} card={tProduct} />
}
...
export const SimplySwiper: FC<props> = ({ index, onUserDislike, onUserLike, products }) => {
return (
<Swiper ref={swiperRef}
cards={products}
cardIndex={index}
renderCard={(card, cardIndex) => renderCard(card, cardIndex)}
onSwipedRight={(num) => onUserLike(products[index])}
onSwipedLeft={(num) => onUserDislike(products[index])}
backgroundColor={'transparent'}
onTapCard={() => onUserDislike(products[index])}
cardVerticalMargin={0}
...
I know the fact that causes this issue. And i have solution for the same. Props are getting changed but state value of swiper in state in cards is not changing so i set below code in shouldComponentUpdate
method. Please make sure you update that and check that again.
if (propsChanged || stateChanged)
this.setState({ cards: props.cards })
I posted the solution that worked for me in the crossposted issue found here: https://github.com/alexbrillant/react-native-deck-swiper/issues/346#issuecomment-800725507
Cross Posted
This issue is cross posted to here as I was not sure which repository is being actively maintained.
Issue
When dynamically adding elements to the Swiper card prop, the newly added properties will not appear on the screen.
Longer Description
My issue comes from the following situation: My data is paginated, so I get the first page of data initially which consists of 5 elements which loads just fine and I am able to swipe on them no problem. I begin to load page two when the user is 2 away from the end of the five, once the data is returned I concat the array returned with page 2 data on to the array of page 1 data. This is where the issue is: The first five from page 1 show perfectly fine and I am able to interact with them flawlessly but when I try to concat page two to the existing array passed into the props of the Swiper the new items dont show up.
Expectation
I would expect when I modify the array being passed into the cards prop that change is reflected in the swiper and I can interact with the newly added array elements
Video
Ignore Ugly UI, its under construction...
https://user-images.githubusercontent.com/26589485/109442265-f091cc00-79f4-11eb-8eaf-132eb5552208.mp4
As you can see in the video the total property correctly updates to 10 after the second page of data is loaded in but once I reach the end of the first pages data the swiper fails to show the next 5 elements (the second page)
Related issues I have visited numerous times
153, This Comment, #192, #189, and This Section in the Readme
Code
Swiper
Package.json
If there is anything else you would like to see from me please feel free to let me know. Thank you.