Open In App

Create a Voting App using React-Native

Last Updated : 31 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Voting involves assessing multiple entities based on specific criteria. This article guides the creation of a basic voting app, where users can compare and evaluate options. The application allows users to make choices, making it a valuable tool for decision-making and gathering opinions. We will create a Voting app using React-Native.

Preview Image:

Screenshot-2023-10-07-093759

Prerequisites:

  • Introduction to React Native
  • Introduction React Native Components
  • React Native State
  • React Native Props
  • Expo CLI
  • Node.js and npm (Node Package Manager)

Steps to install & configure React Native:

Step 1: Create a react native application by using this command:

npx create-expo-app basicNotes-app

Step 2: After creating your project folder, i.e. basicNotes-app, use the following command to navigate to it:

cd basicNotes-app

Step 3: Open App.js file, open it and paste the source code into it.

Example: In this example, we create an app displaying images of programming languages with voting buttons. The useState hook is used to manage the vote count, enabling the castVote function to update votes for each image. The app is structured with a ScrollView and styled using the StyleSheet module. Each image is displayed in its own container with a corresponding vote count.

Javascript




import React, { useState } from 'react';
import { StyleSheet, Text, View, Button, Image, ScrollView }
     from 'react-native';
  
const image1 = 
const image2 = 
const image3 = 
const image4 = 
  
const App = () => {
    const [votes, setVotes] = useState({
        image1: 0,
        image2: 0,
        image3: 0,
        image4: 0,
    });
  
    const castVote = (imageKey) => {
        setVotes((prevVotes) => ({
            ...prevVotes,
            [imageKey]: prevVotes[imageKey] + 1,
        }));
    };
  
    return (
        <ScrollView contentContainerStyle={styles.container}>
            <Text style={styles.title}>GeeksforGeeks</Text>
  
            <View style={styles.imageBtnContainer}>
                <View style={styles.imageContainer}>
                    <Image source={{ uri: image1 }} 
                        style={styles.image} />
                    <Button
                        title="Vote for HTML"
                        onPress={() => castVote('image1')}
                        style={styles.button}
                    />
                    <View style={styles.voteContainer}>
                        <Text style={styles.voteText}>Votes: 
                         {votes.image1}</Text>
                    </View>
                </View>
  
                <View style={styles.imageContainer}>
                    <Image source={{ uri: image2 }}
                       style={styles.image} />
                    <Button
                        title="Vote for CSS"
                        onPress={() => castVote('image2')}
                        style={styles.button}
                    />
                    <View style={styles.voteContainer}>
                        <Text style={styles.voteText}>Votes:
                          {votes.image2}</Text>
                    </View>
                </View>
            </View>
  
            <View style={styles.imageBtnContainer}>
                <View style={styles.imageContainer}>
                    <Image source={{ uri: image3 }}
                       style={styles.image} />
                    <Button
                        title="Vote for Javascript"
                        onPress={() => castVote('image3')}
                        style={styles.button}
                    />
                    <View style={styles.voteContainer}>
                        <Text style={styles.voteText}>Votes: 
                        {votes.image3}</Text>
                    </View>
                </View>
  
                <View style={styles.imageContainer}>
                    <Image source={{ uri: image4 }} 
                           style={styles.image} />
                    <Button
                        title="Vote for React"
                        onPress={() => castVote('image4')}
                        style={styles.button}
                    />
                    <View style={styles.voteContainer}>
                        <Text style={styles.voteText}>
                            Votes: {votes.image4}
                        </Text>
                    </View>
                </View>
            </View>
        </ScrollView>
    );
};
  
const styles = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        backgroundColor: '#D2E0FB',
    },
    title: {
        fontSize: 20,
        fontWeight: 'bold',
        marginVertical: 20,
        color: 'green'
    },
    imageBtnContainer: {
        flexDirection: 'row',
        justifyContent: 'space-between',
        marginBottom: 10,
    },
    imageContainer: {
        alignItems: 'center',
        marginRight: 10,
    },
    image: {
        width: 150,
        height: 150,
        resizeMode: 'cover',
        marginBottom: 10,
    },
  
    voteContainer: {
        backgroundColor: '#8EACCD',
        padding: 5,
        borderRadius: 5,
        marginTop: 10
    },
    voteText: {
        fontSize: 16,
        fontWeight: 'bold',
        color: 'white',
    },
});
  
export default App;


Step 4: Go to the Terminal and type the following command to run the react native application.

npx expo start

To run on Android:

npx react-native run-android

To run on IOS:

npx react-native run-ios

Output:

gfg



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads