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
  • React Tutorial
  • React Exercise
  • React Basic Concepts
  • React Components
  • React Props
  • React Hooks
  • React Router
  • React Advanced
  • React Examples
  • React Interview Questions
  • React Projects
  • Next.js Tutorial
  • React Bootstrap
  • React Material UI
  • React Ant Design
  • React Desktop
  • React Rebass
  • React Blueprint
  • JavaScript
  • Web Technology
Open In App
Next Article:
Create a Random Quote Generator using React-Native
Next article icon

Random Quote Generator App using ReactJS

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

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 displayed beautifully using CSS styling to create a good user interface.

Let us have a look at how the final application will look like:

Screenshot-(198)

Prerequisites/Tecnologies Used

  • React
  • JSX
  • class-based components
  • lifecycle of components

Approach:

We’re going to use React on the front end and we’ll make get requests to Advice Slip JSON API. After going through this article, you will have a strong understanding of basic React workflow as well as how to make API requests in React Apps. Learn how to fetch API data with React js.  

Advice Slip JSON API: https://api.adviceslip.com/

Steps to create the application:

 Step 1: create react app by the following command

npx create-react-app quote-generator-react

Step 2: Now, go to the folder

cd quote-generator-react

Step 3: Install the required dependencies

npm i axios

Project Structure: It will look like the following.

The updated dependencies in package.json will look like:

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.4.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

Example: Write the following code in respective files.

  • index.html: This file is used to import font package and change the title
  • App.js:  In this, we will create a class-based App component in this app component we are going to have a State
  • App..css: This file contains our styling code for our random quote generator app. 
HTML
<!--index.html-->  <!DOCTYPE html> <html lang="en">  <head>     <meta charset="utf-8" />     <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />     <meta name="viewport" content=         "width=device-width, initial-scale=1" />     <meta name="theme-color" content="#000000" />     <meta name="description" content=         "Web site created using create-react-app" />     <link rel="apple-touch-icon"         href="%PUBLIC_URL%/logo192.png" />      <!--Fonts-->     <link rel="preconnect" href="https://fonts.gstatic.com" />     <link href= "https://fonts.googleapis.com/css2?family=Spartan:wght@100;200;300;             400;500;600;700;800;900&display=swap"             rel="stylesheet" />      <title>Quote Generator</title> </head>  <body>     <noscript>         You need to enable JavaScript         to run this app.     </noscript>     <div id="root"></div> </body>  </html> 
CSS
body {   display: flex;   justify-content: center;   align-items: center;   height: 100vh;   margin: 0;   background: linear-gradient(to right, #ece9e6, #ffffff);   font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }  .app {   text-align: center; }  .card {   background: #fff;   padding: 40px 20px;   border-radius: 15px;   box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);   max-width: 400px;   margin: 20px; }  .heading {   font-size: 28px;   color: #333;   margin-bottom: 30px;   position: relative;   padding-bottom: 10px; }  .heading::after {   content: "";   position: absolute;   width: 50px;   height: 3px;   background-color: #4caf50;   bottom: 0;   left: 50%;   transform: translateX(-50%); }  .button {   background-color: #4caf50;   color: white;   border: none;   padding: 15px 30px;   font-size: 18px;   border-radius: 25px;   cursor: pointer;   transition: background-color 0.3s ease;   text-transform: uppercase;   letter-spacing: 1px; }  .button:hover {   background-color: #45a049; }  span {   display: block;   font-size: 18px;   font-weight: bold;   margin-top: 20px; } 
JavaScript
import React from "react"; import axios from "axios"; import "./App.css";  class App extends React.Component {     state = { advice: "" };      componentDidMount() {         this.fetchAdvice();     }      fetchAdvice = () => {         axios             .get("https://api.adviceslip.com/advice")             .then((response) => {                 const { advice } = response.data.slip;                 this.setState({ advice });             })             .catch((error) => {                 console.log(error);             });     };      render() {         const { advice } = this.state;          return (             <div className="app">                 <div className="card">                     <h1 className="heading">{advice}</h1>                     <button className="button" onClick={this.fetchAdvice}>                         <span>Give Me Advice</span>                     </button>                 </div>             </div>         );     } }  export default App; 

Steps to run the application:


Step 1: Type the following command in your command line

npm start

Step 2: Open http://localhost:3000/ URL in the browser. It will display the result.

Output:

a1


Next Article
Create a Random Quote Generator using React-Native
author
bhartik021
Improve
Article Tags :
  • Blogathon
  • Project
  • ReactJS
  • Web Technologies
  • Blogathon-2021
  • ReactJS-Projects
  • Web Development Projects

Similar Reads

  • 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 Random Quote Generator using React-Native
    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 u
    4 min read
  • Build a Random User Generator App Using ReactJS
    In this article, we will create a random user generator application using API and React JS. A Random User Generator App Using React Js is a web application built with the React.js library that generates random user profiles. It typically retrieves and displays details like names, photos, and contact
    4 min read
  • Create a Random User Generator using jQuery
    With the use of the API and jQuery, we'll create a random user generator app. A straightforward web application called "jQuery Random User Generator" makes use of jQuery and the RandomUser.me API to generate random user data and present it in a visually appealing way. Users of this project can click
    3 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
  • Color Palette Generator app using React
    Color Palette Generator App using ReactJS is a web application which enables use­rs to effortlessly gene­rate random color palettes, vie­w the colors, and copy the color codes to the­ clipboard with just a single click. There is also a search bar which allows the user to check different color themes
    5 min read
  • Create a QR code generator app using ReactJS
    In this article, we will create a simple QR Code generator app. A QR code is a two-dimensional barcode that can be read by smartphones. It allows the encoding of more than 4,000 characters in a compact format. QR codes can be used for various purposes, such as displaying text to users, opening URLs,
    3 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
  • Quote Generator App using NextJS
    In this article, we will build an quote generator using NextJS. The user will be given a button that, when clicked, will retrieve a random quote from the API and display it on the screen. By clicking the button again, users can generate a large number of advices. Technologies Used/PrerequisitesIntro
    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
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