question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to do asynchronous concatenating

See original GitHub issue

Thanks for the awesome library. I am trying to fetch more cards when the cards are almost finished to increase performance. State gets updated with new concatenated cards but the swiper doesn’t render new cards. I am attaching code of my component for your reference. Can you please check and suggest what can I do to fix it. This is the code of my file:-

`import React, { Component } from ‘react’ import Swiper from ‘react-native-deck-swiper’ import { Button, StyleSheet, Text, View, Image } from ‘react-native’ import { Card, ListItem, Icon } from ‘react-native-elements’ import { DataCall } from ‘…/utils/DataCall’; import { Loading } from ‘./LoadingComponent’; import Pure from ‘./PureComponent’;

// demo purposes only function * range (start, end) { for (let i = start; i <= end; i++) { yield i } }

class Exemple extends Component { constructor (props) { super(props) this.state = { swipedAllCards: false, swipeDirection: ‘’, cardIndex: 0, cards: [], count: 1 } this.inProgressNetworkReq = false; }

componentWillMount() {
    this.fetchInitialData();
}

componentDidMount(){
    this.mounted = true;
}
async fetchInitialData() {
    //To prevent redundant fetch requests. Needed because cases of quick up/down scroll can trigger onEndReached
    //more than once
    this.inProgressNetworkReq = true;
    const cards = await DataCall.get(this.state.count, this.props.category, 9);
    this.inProgressNetworkReq = false;
    if(cards && this.mounted){
        this.setState({
            cards: this.state.cards.concat(cards),
            count: this.state.count + 1,
        });
    }
}

async fetchMoreData() {
    const cards = await DataCall.get(this.state.count, this.props.category, 9);
    if(cards){
        this.setState({
            cards: this.state.cards.concat(cards)
        })
    }
}

renderCard = (dish, index) => {
    if(dish){
        return (
            <Pure dish={dish} index={index} />
        )
    }
};

onSwiped = (index) => {
    console.log(`on swiped ${index}`)

    if(index==3){
        this.fetchMoreData();
    }
}

onSwipedAllCards = () => {
    this.setState({
        swipedAllCards: true
    })
};

swipeLeft = () => {
    this.swiper.swipeLeft()
};

render () {
    if(this.inProgressNetworkReq){
        return(
            <Loading/>
        )
    }
    else{
        return (
            <View style={styles.container}>
                <Swiper
                    ref={swiper => {
                        this.swiper = swiper
                    }}
                    horizontalSwipe = {false}
                    onSwiped={(index) => this.onSwiped(index)}
                    cards={this.state.cards}
                    cardIndex={this.state.cardIndex}
                    cardVerticalMargin={0}
                    renderCard={this.renderCard}
                    onSwipedAll={this.onSwipedAllCards}
                    stackSize={3}
                    stackSeparation={15}
                    overlayLabels={{
                        bottom: {
                            title: 'BLEAH',
                            style: {
                                label: {
                                    backgroundColor: 'black',
                                    borderColor: 'black',
                                    color: 'white',
                                    borderWidth: 1
                                },
                                wrapper: {
                                    flexDirection: 'column',
                                    alignItems: 'center',
                                    justifyContent: 'center'
                                }
                            }
                        },
                        left: {
                            title: 'NOPE',
                            style: {
                                label: {
                                    backgroundColor: 'black',
                                    borderColor: 'black',
                                    color: 'white',
                                    borderWidth: 1
                                },
                                wrapper: {
                                    flexDirection: 'column',
                                    alignItems: 'flex-end',
                                    justifyContent: 'flex-start',
                                    marginTop: 30,
                                    marginLeft: -30
                                }
                            }
                        },
                        right: {
                            title: 'LIKE',
                            style: {
                                label: {
                                    backgroundColor: 'black',
                                    borderColor: 'black',
                                    color: 'white',
                                    borderWidth: 1
                                },
                                wrapper: {
                                    flexDirection: 'column',
                                    alignItems: 'flex-start',
                                    justifyContent: 'flex-start',
                                    marginTop: 30,
                                    marginLeft: 30
                                }
                            }
                        }
                    }}
                    animateOverlayLabelsOpacity
                    animateCardOpacity
                    swipeBackCard
                >
                </Swiper>
            </View>
        )
    }
}

}

const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: ‘#F5FCFF’ } })

export default Exemple; `

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
dochathuccommented, Mar 25, 2019

Until v1.5.26, this library had componentWillReceiveProps method, however for some reasons it has been removed. I believe most developers are fetching new cards on swipe therefore a PR would be a better solution for the community instead of a fork. @dochathuc What do you think?

@markhomoki Yes, I have just created a fork yesterday. I’ll test more with the fork and then create a PR if it is fine.

0reactions
mightymcommented, Jun 23, 2019

Double checked my issue. In the current version shouldComponentUpdate receives the new array of cards as props. And also return true but the card stack itself doesn’t update. https://github.com/alexbrillant/react-native-deck-swiper/blob/master/Swiper.js#L81

Read more comments on GitHub >

github_iconTop Results From Across the Web

Client JS: Return concatenation of async requests
How to write a function which sends asynchronously requests to a given array of URLs, concatenates the result of each request and returns...
Read more >
Async/Await, Combine, Closures: A Guide to Modern ...
A comparison between async/await, Combine, and closures in Swift. Includes an overview of the strengths and weaknesses of the different ...
Read more >
How to use promises - Learn web development | MDN
With a promise-based API, the asynchronous function starts the operation and returns a Promise object. You can then attach handlers to this ...
Read more >
Testing-library: avoid these mistakes in async tests
Our changes made perfect sense, but suddenly our test will start to fail with "Unable to find an element with the text: Alice...
Read more >
How do I concatenate multiple async IEnumerable methods?
I want to concatenate the outputs of several async methods into a ... If I could yield the results, that'd be great, but...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found