Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NextJS
  • Material UI
  • React Bootstrap
  • React Suite
  • Ant Design
  • Reactstrap
  • BlueprintJS
  • React Desktop
  • React Native
  • React Rebass
  • React Spring
  • React Evergreen
  • ReactJS
  • ReactJS
  • JS Formatter
  • Web Technology
Open In App
Next Article:
Create a Memory Pair Game using React-Native
Next article icon

Create a Random Quote Generator using React-Native

Last Updated : 24 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

React Native is the most flexible and powerful mobile application development framework, which has various features embedded into it. Using this framework, we can create different interactive applications. Creating a Random Quote Generator using React Native is one of the interactive project which utilizes the API to generate the random quote and present it to the user.

In the application, the user can click the button to fetch the random quote from the API. Along with this, the user can click on the copy button to copy the quote to the clipboard for further use. We have styled the application using CSS styling embedded in the JS file

Preview of final output: Let us have a look at how the final output will look like.

WhatsApp-Image-2023-11-30-at-121756-PM

Prerequisites:

  • React Native
  • React Native Components
  • React Native State
  • React Native Props
  • Expo CLI
  • NodeJs

Approach:

The project code is completely written in React Native language which allows the user to look for Random quotes and copy them for further use. We have used the useState hook to manage the state of the application and the useEffect hook is used to fetch a random quote from the API when the component mounts. It also ensures that the fetchQuoteFunction is called only once when the component is initially rendered. The application has the functionality to copy the quote to the clipboard and use it in different sources.

Steps to Create React Native Application:

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

npx create-expo-app random-quote

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

cd random-quote

Project Structure:

PS

The updated dependencies in package.json file will look like:

"dependencies": {
"@expo/webpack-config": "^19.0.0",
"@react-native-async-storage/async-storage": "^1.21.0",
"@react-native-clipboard/clipboard": "^1.12.1",
"@react-navigation/native": "^6.1.9",
"@react-navigation/stack": "^6.3.20",
"axios": "^1.6.2",
"expo": "~49.0.15",
"expo-av": "^13.6.0",
"expo-status-bar": "~1.6.0",
"react": "18.2.0",
"react-native": "0.72.6",
"react-native-clipboard": "^0.0.5",
"react-native-vector-icons": "^10.0.2",
"react-native-web": "~0.19.6"
}

Example: Below is the implementation of the above-discussed approach

JavaScript
// App.js  import React, { useState, useEffect } from "react";  import {View,Text,TouchableOpacity,ScrollView,Platform,Clipboard} from "react-native";  import Icon from "react-native-vector-icons/FontAwesome";  import axios from "axios";  import AsyncStorage from "@react-native-async-storage/async-storage";  import styles from "./styles";   const App = () => {  	const [quote, setQuote] = useState("");  	useEffect(() => {  		fetchQuoteFunction();  	}, []);  	const fetchQuoteFunction = async () => {  		try {  			const res = await axios.get("https://type.fit/api/quotes");  			const quo = res.data;  			const idx = Math.floor(Math.random() * quo.length);  			const randomQuo = quo[idx].text;  			await AsyncStorage.setItem("lastQuote", randomQuo);  			setQuote(randomQuo);  		} catch (error) {  			console.error("Error fetching quotes:", error.message);  		}  	};  	const newQuoteFunction = () => {  		fetchQuoteFunction();  	};  	const cpyFunction = () => {  		try {  			Clipboard.setString(quote);  			alert("Quote copied to clipboard!");  		} catch (error) {  			console.error("Error copying to clipboard:", error.message);  		}  	};  	return (  		<View style={styles.container}>  			<ScrollView contentContainerStyle={styles.scrollViewContent}>  				<Text style={styles.geeksText}>GeeksforGeeks</Text>  				<Text style={styles.header2}>  					Random Quote Generator using React-Native  				</Text>  				<View style={styles.card}>  					<Icon  						name="quote-left" 						size={30}  						color="#3498db" 						style={styles.quoteIcon}  					/>  					<Text style={styles.quoteText}>{quote}</Text>  					<View style={styles.cardButtons}>  						<TouchableOpacity  							style={styles.copyButton}  							onPress={cpyFunction}  						>  							<Text style={styles.copyButtonText}>  								Copy to Clipboard  							</Text>  						</TouchableOpacity>  						<TouchableOpacity  							style={styles.newQuoteButton}  							onPress={newQuoteFunction}  						>  							<Text style={styles.buttonText}>New Quote</Text>  						</TouchableOpacity>  					</View>  				</View>  			</ScrollView>  		</View>  	);  };  export default App;  
JavaScript
//styles.js   import { StyleSheet } from "react-native";  const styles = StyleSheet.create({  	container: {  		flex: 1,  		backgroundColor: "#ecf0f1",  		paddingHorizontal: 20,  		justifyContent: "center",  		alignItems: "center",  	},  	scrollViewContent: {  		flexGrow: 1,  		justifyContent: "center",  		alignItems: "center",  	},  	header2: {  		fontSize: 16,  		marginBottom: 20,  		color: "black",  		fontWeight: "bold",  	},  	geeksText: {  		color: "green",  		fontSize: 30,  		fontWeight: "bold",  		textAlign: "center",  		marginBottom: 10,  	},  	card: {  		backgroundColor: "#fff",  		borderRadius: 10,  		padding: 20,  		marginBottom: 20,  		elevation: 5,  		shadowColor: "#000",  		shadowOffset: { width: 0, height: 2 },  		shadowOpacity: 0.3,  		shadowRadius: 3,  	},  	quoteIcon: {  		marginBottom: 10,  		alignSelf: "center",  	},  	quoteText: {  		fontSize: 18,  		textAlign: "center",  		marginBottom: 20,  		fontStyle: "italic",  		fontWeight: "bold",  		color: "#555",  	},  	cardButtons: {  		flexDirection: "row",  		justifyContent: "flex-end",  		marginTop: 10,  	},  	copyButton: {  		backgroundColor: "blue",  		padding: 10,  		borderRadius: 5,  		marginRight: 30,  	},  	copyButtonText: {  		color: "#fff",  		fontSize: 16,  		fontWeight: "bold",  		textAlign: "center",  	},  	newQuoteButton: {  		backgroundColor: "red",  		padding: 10,  		borderRadius: 5,  	},  	buttonText: {  		color: "#fff",  		fontSize: 16,  		fontWeight: "bold",  		textAlign: "center",  	},  });  export default styles;  

Steps to run the application

Step 1: Go to the Terminal and type the following command to run the React native application.

npx expo start

Step 2: Depending on your operating system type the following command

  • To run on Android:
npx react-native run-android
  • To run on IOS:
npx react-native run-ios

Output:


Next Article
Create a Memory Pair Game using React-Native

G

gpancomputer
Improve
Article Tags :
  • Project
  • Web Technologies
  • ReactJS
  • Geeks Premier League
  • React-Native
  • Geeks Premier League 2023

Similar Reads

  • Random Quote Generator App using ReactJS
    In this article, we will create an application that uses an API to generate random quotes. The user will be given a button which on click will fetch a random quote from the API and display it on the screen. Users can generate many advices by clicking the button again. The button and the quotes are d
    3 min read
  • Create Memes Generator App using React-Native
    The Me­me Generator App is a mobile­ application that allows users to effortlessly create memes. With its use­r-friendly interface, use­rs can choose from a wide collection of popular me­me templates and add their own customized text to the top and bottom. In this article, we will see how we can bui
    4 min read
  • Create Jokes Generator App using React-Native
    In this article, we are going to build a jokes generator app using react native. React Native enables you to master the­ designing an elegant and dynamic use­r interface while e­ffortlessly retrieving joke­s from external APIs. Let's take a look at how our completed project will look Prerequisites /
    3 min read
  • Build a Random Name Generator using ReactJS
    In this article, a Random Name Ge­nerator will be created using React.js. Building a Random Name Generator means creating a program or application that generates random names, typically for various purposes like usernames, fictional characters, or data testing. It usually involves combining or selec
    4 min read
  • Create a Memory Pair Game using React-Native
    In this article, we will build the interactive Memory Pair Game using the React Native language. We are displaying the 12 boxes to the user, in the user has to click on each box, and when the user clicks on the box the random icon will be shown. Users have to guess or find its match by clicking on t
    5 min read
  • Create a Password Manager using React-Native
    This article will demonstrate how to create a Password Manager Application using React-Native. To assist users in securely storing and managing their passwords, we will develop a Password Manager mobile­ application using React Native for this project. The application will provide functionalities su
    6 min read
  • Create a meme generator by using ReactJS
    In this tutorial, we’ll create a meme generator using ReactJS. In the meme generator, we have two text fields in which we enter the first text and last text. After writing the text when we click the Gen button, it creates a meme with an image and the text written on it. Preview Image: PrerequisiteTh
    3 min read
  • Create an OTP Generator and Validator App using React-Native
    One-time passwords (OTPs) have become a popular choice for enhancing the security of various online services and applications. In this article, we'll explore how to create an OTP Generator and Validator App using React Native, a popular framework for building cross-platform mobile applications. Prev
    4 min read
  • Create an Interactive Quiz App using React-Native?
    In this article, we are going to implement an Interactive Quiz App using React Native. Interactive Quiz App is a mobile application that allows users to take tests and view their quiz results to see how well they performed. This Interactive Quiz App consists of multiple questions and multiple-choice
    4 min read
  • Create a Number Guessing App using React-Native
    The Number Guessing App is a simple mobile game where the user tries to guess a random number generated by the app. The app provides feedback to the user, indicating whether their guess is too high or too low, and keeps track of the number of attempts it takes to guess the correct number. In this ar
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences