The Like and increment buttons seem to be unresponsive when placed within a FlatList component

Issues with the like and increment button functionality within the FlatList

Here are my constructor, increment, and like functions:

constructor(props){
    super(props);
    this.state = {
        count: true,
        count1: 0,
    };
}
onlike = () => {
    this.setState(state => ({
        count: !state.count
    }));
}
_incrementCountNew = () => {
    let newCount1 = this.state.count1 + 1
    this.setState({
        count1: newCount1
    })
}

<FlatList
    data={data}[
        renderItem = {({ item }) => (
            <View style={styles.Text2}>
                <Image
                    source={item.image}
                    style={{ width: 329, height: 250, marginRight: 0 }} />

                <Text style={{ marginLeft: 10, fontSize: 15, marginTop: 10 }}>
                    {item.content}
                </Text>

                <View style={{ marginLeft: 10, marginTop: 10, marginRight: 250, marginBottom: 1 }}>
                    <TouchableOpacity>
                        <Icon2
                            name={item.vectoricon}
                            size={20}
                            color="#b3b3b3"
                        />
                    </TouchableOpacity>
                    <Text style={{ marginLeft: 25, marginTop: -20, color: "#b3b3b3" }}>{this.state.count ? '0' : '1'}</Text>
                </View>
                <View style={{ marginLeft: 90, marginTop: -20, marginBottom: 1, marginRight: 70 }}>
                    <TouchableOpacity>
                        <Icon1
                            name={item.vectoricon1}
                            size={20}
                            color="#b3b3b3"
                            onPress={this._incrementCountNew} />
                    </TouchableOpacity>
                    <Text style={{ marginLeft: 25, marginTop: -20, color: "#b3b3b3" }}>{this.state.count1}</Text>
                </View>

                <View style={styles.lineStyle} />

                <View style={{ marginLeft: 10, marginTop: 1, marginRight: 270 }}>
                    <TouchableOpacity onPress={this._incrementCountNew}>
                        <Icon1
                            name={item.vectoricon1}
                            size={30}
                        />
                    </TouchableOpacity>
                </View>

                <View style={{ marginLeft: 60, marginTop: -30, marginBottom: 10, marginRight: 210 }}>
                    <TouchableOpacity onPress={this.onlike}>
                        <View>
                            {this.state.count ? <Icon3 name={item.vectoricon2} size={30} /> : <Icon3 name={item.vectoricon3} size={30} />}
                        </View>
                    </TouchableOpacity>
                </View>

            </View>
        )}

        keyExtractor = { this.keyExtractor } />

Answer №1

When utilizing a FlatList, it's important to consider giving each item in your list its own like/unlike and upvote counter.

If you want this customization, then it wouldn't be logical to connect the vote count to a single variable in your state, correct?

Furthermore, there are ways to enhance the increment function for better performance, such as:

_updateVoteCount = () => {
      this.setState({
         likeCount: this.state.likeCount + 1
     })
}

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

Is it feasible to update just one key within an exported type using setState in TypeScript?

Recently, I decided to dive into Typescript within my React App and encountered an error that has left me stumped. I'm wondering if it's possible to access a specific key in an exported type and modify its value? Here is how I've exported ...

What is the best way to prevent the content within a div from being obscured by a sticky element affixed to the bottom of the div?

I am struggling with ensuring that the content in my wrapper does not get cut off by the "view more" div attached to the bottom. The default wrapper cuts off the children, and even when expanded, the issue persists due to CSS problems. In React, the gener ...

What is the best way to add permissions to each role in JavaScript?

I have been attempting to dynamically add data to an HTML table using JavaScript. The data consists of roles and their corresponding permissions, which are retrieved using Laravel's ORM. I have tried utilizing a nested each jQuery function to append t ...

Hidden warning to React-select for being uncontrolled

I've integrated react-select into my code: import React, {Component} from 'react'; import Select, {createFilter} from 'react-select'; let _ = require('underscore') class Test extends Component { constructor(props) ...

angular2 : problem encountered with communication to rest api

Transitioning from PHP to Angular2 has been quite challenging for me, especially when trying to use a real rest API like "Tour of Heroes". I initially thought it would be simple... Currently, I have set up a functional API with Express: curl -XGET http:/ ...

The default value of components in Next.js

I'm working on establishing a global variable that all components are initially rendered with and setting the default value, but I'm unsure about how to accomplish the second part. Currently, this is what I have in my _app.tsx: import { AppProps ...

When making an HTTP GET request followed by another GET request in Express, it results in an error with undefined parameters on the

When I open a form that has a link to a list and try to access the list, I encounter an "id" undefined error for the form we came from, which was already functional. The issue arises when I have a GET page where I present a form to modify a record at /loc ...

CSS Flexibility in Action

Presently, my tab bar has a fixed look as shown here: https://codepen.io/cdemez/pen/WNrQpWp Including properties like width: 400px; etc... Upon inspecting the code, you'll notice that all the dimensions are static :-( Consequently, I am encountering ...

What could be causing the fourth tab to not show any data while the first three tabs are functioning as expected?

I've encountered an issue with my HTML tabs. While I can easily switch between the first three tabs and view their content, tab 4 doesn't seem to display its associated data. It's puzzling why tab 4 is not working when the others are functio ...

Notifying Users of Payment Updates

In the process of developing our application, we integrated a Payment gateway called flutterwave. Every time a payment is successful or fails, a webhook is triggered which prompts us to send notifications through emails, SMS, and update payment statuses in ...

Node 18 is having trouble locating NPM and is unable to locate the module './es6/validate-engines.js'

Previously, I attempted to install Node.js without any issues. However, this time around, I am encountering difficulties accessing the npm package. While I can successfully retrieve the version of Node.js after installation, attempting to check npm -v or w ...

Encountering an "Angularjs 1.2.x Injector:modulerrr" error, even after successfully incorporating ngRoute

I am currently learning angularjs and I have encountered a persistent error. Despite multiple attempts at troubleshooting, I have been unable to resolve it. Here is the code that I have: index.html <!doctype html> <html ng-app="myApp"> <he ...

Issue with jQuery hide() function in Internet Explorer 9

I need help with creating a hidden div that becomes visible when a user clicks a link. The code I have works in FF / IE7 / IE8 but not in IE9, where the div is always visible without any content. Any advice is appreciated! <script> $(document).r ...

The Mui Datepicker displays red text as a warning before validation

Upon utilizing the Mui Date picker and date time picker, I am encountering an issue where the red text appears upon opening the modal: https://i.stack.imgur.com/SdgtV.png I aim to remove the red text unless an incorrect date is entered, but I am unable t ...

Cassandra encountered a TypeError stating that the "value" argument is exceeding the bounds

I keep encountering the error message below. Any thoughts on what might be causing it? TypeError: "value" argument is out of bounds at checkInt (buffer.js:1041:11) at Buffer.writeInt32BE (buffer.js:1244:5) at Encoder.encodeInt (/ ...

Ways to achieve a gradient effect on top of an image

Hey, I'm trying to implement a gradient on images in my project. Here's what I have so far: import Image from 'next/image' import Box from 'mui/material/Box' import PlayIcon from './PlayIcon.tsx' <Box ...

Repetitive data in a list with AngularJS

There's probably a simple solution to this: I have some data in a controller, and depending on whether an item is selected or not, it should display different HTML. If the item is selected, the HTML should be: <li> <a href="#"><s ...

Website API: decouple backend and frontend functionality through an API

I am currently working on the development of a website and an app created through Cordova. The app will essentially mirror the functionalities of the website. The website is already established and heavily relies on JavaScript, with potential considerati ...

Executing a for loop just once in JavaScript

var door1 = {open: false,car: false,choose: false}; var door2 = {open: false,car: false,choose: false}; var door3 = {open: false,car: false,choose: false}; var carName = 0; var objectName = 0; var goatName = 0; var min = 1; var max = 4; var random = Math. ...

Tips on Modifying JSON Objects with JavaScript

In the code I'm working with, there's a generated line that creates an animated sidebar using a div. The width of this sidebar is controlled by the 'v' parameter, currently set to 85. <div id="sidebar" class="inner-element uib_w_5 ...