From 3fab4b7833232aad66be31192c160ba120c00970 Mon Sep 17 00:00:00 2001 From: Sumanth U <157620444+Sumanth077s@users.noreply.github.com> Date: Tue, 26 Nov 2024 10:30:41 +0530 Subject: [PATCH] Update App.js --- 08-lorem-ipsum/final/src/App.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/08-lorem-ipsum/final/src/App.js b/08-lorem-ipsum/final/src/App.js index 4ff2d0063..43231cb04 100644 --- a/08-lorem-ipsum/final/src/App.js +++ b/08-lorem-ipsum/final/src/App.js @@ -1,36 +1,56 @@ +// Import React and the useState hook for state management import React, { useState } from 'react'; + +// Importing the data array which contains the paragraphs to be displayed import data from './data'; + function App() { + // State to keep track of the number of paragraphs to generate const [count, setCount] = useState(0); + + // State to store the paragraphs to display const [text, setText] = useState([]); + // Event handler for the form submission const handleSubmit = (e) => { - e.preventDefault(); + e.preventDefault(); // Prevent the form from refreshing the page + + // Convert the count input value to an integer let amount = parseInt(count); + + // Ensure the count is at least 1 and at most 8 if (count <= 0) { amount = 1; } if (count > 8) { amount = 8; } + + // Update the text state with the sliced data array based on the amount setText(data.slice(0, amount)); }; + return (

tired of boring lorem ipsum?

+ {/* Form for selecting the number of paragraphs */}
+ {/* Input for the number of paragraphs */} setCount(e.target.value)} + onChange={(e) => setCount(e.target.value)} // Update count state on input change />
+ + {/* Display the generated paragraphs */}
{text.map((item, index) => { + // Render each paragraph in a

tag with a unique key return

{item}

; })}