Facing issue with React Native Flip Card functionality not working properly when trying to flip

I am currently working on developing a flip card game using GestureFlipView for the flip card animation. My goal is to display these flip cards in a 3X3 grid by utilizing components from React Native. However, I have encountered an issue where the cards are not flipping as expected and displaying unpredictable behavior. Only the last card seems to be functioning correctly while the others exhibit erratic behavior.

If you'd like to check out my project on Github, here's the repository link: https://github.com/akhilomar/React-Native-Number-Game

Here is a snapshot of the CardScreen component: https://i.sstatic.net/Cliww.png

Card Component Code:

import {View, Text, SafeAreaView, TouchableOpacity} from 'react-native';
import GestureFlipView from 'react-native-gesture-flip-card';

const Cards = (props) => {
    // code snippet

}

export default Cards;

Card List Component Code:

import React from 'react';
import {SafeAreaView, View, FlatList, Dimensions, StyleSheet } from 'react-native';
import Cards from './Cards';

const CardScreen = () => {
    // code snippet

}

export default CardScreen;

Answer №1

Make sure to utilize the ref properly. Additional information can be found here

const Cards = (props) => {
  //begin by defining the ref
  const flipViewRef = React.useRef();


  //use it in onPress like this
  <TouchableOpacity onPress = {() => {
        flipViewRef.current.flipRight() 
        ...     
 }} >

  //assign it in GestureFlipView like this
  <GestureFlipView ref={flipViewRef} />
  


}

Answer №2

The main issue causing difficulties is the utilization of a this reference inside a functional component. As detailed here, the interpretation of this will depend on how the function is invoked and could potentially be undefined. A more dependable method to utilize this is within a class context. In the case of React, this entails using a class component instead of a functional one, which is what's currently being utilized. For further information on function and class components, you can refer here.

It's also worth considering if employing a FlatList is suitable in this scenario. Typically, this component is employed to enhance performance for rendering extensive lists. Instead of implementing a FlatList, it might be advisable to opt for a simpler alternative like utilizing a series of View elements to display the cards. Below is a comprehensive example derived from your code:

// Code snippet provided as an example
import React, { useState } from 'react';
import { View, Dimensions, StyleSheet, Text, TouchableOpacity } from 'react-native';
import GestureFlipView from 'react-native-gesture-flip-card';

// Remaining code defining Card and related components
.
.
.

export default CardScreen;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing my bootstrap-switch to malfunction?

Here is the snippet of code I am working with: jQuery.each($('.onoffswitch-checkbox'), function(i, slotData) { console.log($(slotData).bootstrapSwitch('state')) }) <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1 ...

A solution for Array.includes to handle NaN values

While browsing some Javascript blogs, I stumbled upon the Array prototype methods .indexOf and .includes. I noticed that if an array includes NaN as a value, indexOf may not be able to detect it, leaving us with the option of using .includes. However, cons ...

Issue with Vue-Multiselect: Unselecting a group of pre-loaded values is not functioning as expected

My code: https://jsfiddle.net/bgarrison25/tndsmkq1/4/ Html: <div id="app"> <label class="typo__label">Groups</label> <multiselect v-model="value" :options="options" :multiple="true" group-values="libs" g ...

Tips for eliminating double quotes from an input string

I am currently developing an input for a website that will generate a div tag along with its necessary child elements to ensure the website functions correctly. I have a couple of key questions regarding this setup: <!DOCTYPE html> <html> < ...

Encountering numerous errors during npm installation in a react project

Struggling with React as I try to navigate through course files and run npm install, only to be bombarded with countless errors and warnings on every single file. I've double-checked my directory and ensured the presence of a json file. gyp ERR! ...

How can we stop Vuetify from overwriting Bootstrap3's classes when using treeshaking?

I am currently in the process of transitioning an app from jQuery/Bootstrap3 to Vue/Vuetify. My approach is to tackle one small task at a time, such as converting the Navbar into its own Vue component and then gradually updating other widgets. Since the n ...

Retrieve the value of a JavaScript variable

Currently, I am working on developing a web application that utilizes the Google Maps Direction API. Within a variable named summaryPanel.innerHTML, I store essential details about addresses and distances. I need to utilize the distance data for additional ...

Import GraphQL data in gatsby-browser.js (or alternative method, if available)

I am currently facing a challenge with running a GraphQL query within the context of replaceRouterComponent in gatsby-browser.js (reference to Gatsby browser API). However, I am able to access data from Contentful. If there are alternative approaches or s ...

When I adjust the size of my browser, the elements on my HTML page shift around

I'm facing an issue where all my elements move when I resize my browser, and I cannot figure out why. How can I ensure that they stay in their respective positions even when the browser is resized? Is there a CSS solution for this? Thank you! body ...

The replace function fails to recognize Cyrillic characters when combined with the /b flag

Struggling with a persistent issue, I've noticed that my code works perfectly with Latin characters but fails to recognize Cyrillic characters when using jQuery. $('p').each(function() { var $this = $(this); $this.html($this.text().re ...

Expo: A guide on integrating expo code into an existing Android project

I'm looking to enhance my Android app (which is built in standard Java) by allowing users to create their own 3D models. To achieve this, I want to incorporate a 3D viewer within the app so that users can view and interact with their creations. My pl ...

How to trigger a Bootstrap modal using JavaScript instead of a button

Is it possible to trigger and open a bootstrap modal using JavaScript without the use of a button? I need the modal to be opened based on a different JavaScript program's logic. JAVASCRIPT: <script> //... document.getElementById("modalElem ...

Problem with integration of Bootstrap datetime picker in AngularJS directives

I am currently utilizing the Bootstrap Datetime picker to display data from a JSON file in calendar format. The data is first converted into the correct date format before being shown on the calendar, with both a To date and From date available. scope.onH ...

Embed a React component seamlessly within HTML code

Hey there! I have a situation where I'm pulling valid HTML content from the database and I need to replace merge tags with working React components. The HTML is generated using a WYSIWYG editor. Let me give you an example: const link = <a onClick= ...

What is the best way to rearrange columns in bootstrapping version 3

In the process of developing a responsive website with the use of bootstrap 3, I am faced with the task of rearranging columns for the mobile version. The layout of the home page has been attached for reference, along with the desired layout for the mobi ...

When comparing the values of two arrays with undefined property values

Struggling with sorting an array of arrays that works perfectly except when the property value is undefined. Take this example: posts array = {id: "1", content: "test", "likes":[{"user_id":"2","user_name":"test"}] }, {id: "2", content: "test", "likes": ...

Modify the property of an element within an array

I have an array of objects with a certain property. I need to perform some mathematical operations on this property and return a new array. However, my current approach does not seem to be working as expected. array.map(el => { el.count * 2; r ...

Implementing nested functions with a return value in JavaScript

As someone who is transitioning from a beginner to an intermediate programmer, I find myself struggling to grasp the concept of closures and nested functions with return values. Despite my efforts to read up on these topics, I still can't seem to full ...

Extract the list of selected item Id's from the HTML table and transfer them to the Controller

I have a table in my ASP.NET MVC project that displays information about file types and IDs (which are hidden) to the user. When the user selects checkboxes and clicks on the "Send Request Mail" button, I need to retrieve the IDs of the selected rows and ...

Triggering a dynamically created event with the onchange event

I'm currently working on creating an input type file, and here is the code I have: var element = document.createElement("INPUT"); element.type = "file"; element.id = "element" + i; $("#media").append("<br>"); $("#media").append("<br>"); $ ...