Welcome to my website U-GINE MEDIA. In this website I teach tutorials and share source code on some programming (HTML, CSS, JavaScript, React) tutorials.
Before we get started, do well to subscribe to my channel to never miss out on any update that I post every single day.
You can also visit my GitHub where I upload all the code I have.
In this tutorial topic "", we will learn how to create it following these simple easy steps.
Share this to your programmer friends. If y'all learnt something today, make sure to let me in the comments.
Get Source Code.
If interested in watching the video before running the code below, you can click the play button to get started.
Setting Up the Project
Create a Project Folder:
- Make a new folder, for instance, “quote-generator”.
- Open this folder in your VS Code editor.
Initialize the Project:
npm create vite@latest ./ -- --template react
npm install
npm run dev
Modify folder and CSS Files:
- Remove the default assets folder and App.css file.
- Replace the content of index.css with the provided CSS code.
Creating the Components
- QuoteGenerator.jsx
Adding the Codes
import React, { useEffect, useState } from "react";
import copy from "./assets/copy.png";
const QuoteGenerator = () => {
const [quotes, setQuotes] = useState([]);
const [quote, setQuote] = useState("");
const [author, setAuthor] = useState("");
const [isReady, setIsReady] = useState(false);
//Handle Fetch Quote Function
const fetchQuote = async () => {
setIsReady(false);
try {
const url = await fetch(
"https://api.api-ninjas.com/v1/quotes/?X-Api-Key=Gr3u7UZY9JuGrSUKmMnHqg==yPloDwgeiiMYYrDx"
);
const response = await url.json();
setQuotes((q) => {
// Add the new data at the beginning of the array
const updatedArray = [response[0], ...q];
// Check if the array length exceeds 4
if (updatedArray.length > 4) {
// Remove the last item
updatedArray.pop();
}
return updatedArray;
});
setQuote(response[0].quote);
setAuthor(response[0].author);
setIsReady(true);
} catch (err) {
console.log("Error fetching data:", err);
}
};
//Fetch Quote immediately component is mounted
useEffect(() => {
fetchQuote();
}, []);
//Handle Copy Function
const handleCopy = (e) => {
const text =
e.target.parentElement.parentElement.querySelector("p").textContent;
navigator.clipboard.writeText(text);
return alert("Copied");
};
return (
<div className="quote-generator">
<h1>Quote Generator</h1>
{isReady ? (
<div className="quote">
<p>{quote}</p>
<div>
<button onClick={(e) => handleCopy(e)}>copy</button>
<span>{author}</span>
</div>
</div>
) : (
<p className="pre-message">Loading Quote...</p>
)}
{quotes && (
<div className="quotes">
{quotes.map((v, i) => (
<div key={i} className="q">
<p>{v.quote}</p>
<div>
<img onClick={(e) => handleCopy(e)} src={copy} />
<span>{v.author}</span>
</div>
</div>
))}
</div>
)}
<button onClick={fetchQuote}>Generate Quote</button>
</div>
);
};
export default QuoteGenerator;
import React from "react";
import QuoteGenerator from "./QuoteGenerator";
const App = () => {
return <QuoteGenerator />;
};
export default App;
0 Comments
We are happy to hear from you.