Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to change the card when clicking on button #123

Open
harika-zazz opened this issue Nov 9, 2023 · 2 comments
Open

How to change the card when clicking on button #123

harika-zazz opened this issue Nov 9, 2023 · 2 comments

Comments

@harika-zazz
Copy link

Hi, i am using this module, it's working fine. But how to change the card when clicking on the button.
I've no. of questions when user can select the option and click on next button it goes to next question without swiping. How can i achieve this?
Thanks in advance.
Here is my code

  const deckRef = useRef()
  const [cardIndex, setCardIndex] = useState(0)
 function onChangeCardIndex() {
    setCardIndex(cardIndex + 1)
  }
  <Swiper
                ref={deckRef}
                cards={questionsArr}
                renderCard={(card) => (
                    <Question card={card} onSelectedItem={onSelectedItem} selectedAns={selectedAns} />
                )
                }
                onSwiped={(index) => onChangeCardIndex(index)}
                cardIndex={cardIndex}
                backgroundColor={'white'}
                stackSize={3}
            >
               
            </Swiper>

 <Button
                    onPress={() => { onChangeCardIndex(cardIndex) }}
                    title="Next Question">
                    Next Question
                </Button>
@Kawaljeet-AUI
Copy link

@harika-zazz - Were you able to get pass through this scenario? If yes, can you kindly share how.

@sadraEdrisaraki
Copy link

Hello, I've been able to delegate the work of swiping next/previous by using swipeLeft(), swipeRight() & swipeBack() on a ref

Here is my code snippet, hope this is gonna help

`const SwiperComponent = () => {

const swiperRef = React.useRef<Swiper<Person>>(null);
const [swiping, setSwiping] = React.useState<boolean>(false);

const renderCard: SwiperProps<Person>['renderCard'] = (cardData, cardIndex) => {
    return <Card {...cardData} />;
  };

const onSwipeRight = () => {
    if(!swiping){
      setSwiping(true)
      swiperRef.current?.swipeRight();
    }
  }
  
  const onSwipeLeft = () => {
    if(!swiping){
      setSwiping(true)
      swiperRef.current?.swipeLeft();
    }
  }

  const onUndoSwipe = () => {
    if(!swiping){
      setSwiping(true)
      swiperRef.current?.swipeBack();
    }
  }

return (
  <>
    <Swiper
        ref={swiperRef}
        cards={HomeScreenPics}
        renderCard={renderCard}
        infinite
        backgroundColor="white"
        cardHorizontalMargin={0}
        stackSize={2}
        swipeBackCard={true}
        onSwiped={() => setSwiping(false)}
    />
    <ControlButtons 
        onSwipeLeft={onSwipeLeft}
        onSwipeRight={onSwipeRight}
        onUndoSwipe={onUndoSwipe} />
  </>
    
)

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants