Random Quote Generator App using ReactJS
Last Updated :
25 Jul, 2024
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:
Prerequisites/Tecnologies Used
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:
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Sequence Diagrams - Unified Modeling Language (UML) A Sequence Diagram is a key component of Unified Modeling Language (UML) used to visualize the interaction between objects in a sequential order. It focuses on how objects communicate with each other over time, making it an essential tool for modeling dynamic behavior in a system. Sequence diagrams
11 min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read