Skip to content

Commit 3fab4b7

Browse files
authored
Update App.js
1 parent a760753 commit 3fab4b7

File tree

1 file changed

+22
-2
lines changed
  • 08-lorem-ipsum/final/src

1 file changed

+22
-2
lines changed

08-lorem-ipsum/final/src/App.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,56 @@
1+
// Import React and the useState hook for state management
12
import React, { useState } from 'react';
3+
4+
// Importing the data array which contains the paragraphs to be displayed
25
import data from './data';
6+
37
function App() {
8+
// State to keep track of the number of paragraphs to generate
49
const [count, setCount] = useState(0);
10+
11+
// State to store the paragraphs to display
512
const [text, setText] = useState([]);
613

14+
// Event handler for the form submission
715
const handleSubmit = (e) => {
8-
e.preventDefault();
16+
e.preventDefault(); // Prevent the form from refreshing the page
17+
18+
// Convert the count input value to an integer
919
let amount = parseInt(count);
20+
21+
// Ensure the count is at least 1 and at most 8
1022
if (count <= 0) {
1123
amount = 1;
1224
}
1325
if (count > 8) {
1426
amount = 8;
1527
}
28+
29+
// Update the text state with the sliced data array based on the amount
1630
setText(data.slice(0, amount));
1731
};
32+
1833
return (
1934
<section className='section-center'>
2035
<h3>tired of boring lorem ipsum?</h3>
36+
{/* Form for selecting the number of paragraphs */}
2137
<form className='lorem-form' onSubmit={handleSubmit}>
2238
<label htmlFor='amount'>paragraphs:</label>
39+
{/* Input for the number of paragraphs */}
2340
<input
2441
type='number'
2542
name='amount'
2643
id='amount'
2744
value={count}
28-
onChange={(e) => setCount(e.target.value)}
45+
onChange={(e) => setCount(e.target.value)} // Update count state on input change
2946
/>
3047
<button className='btn'>generate</button>
3148
</form>
49+
50+
{/* Display the generated paragraphs */}
3251
<article className='lorem-text'>
3352
{text.map((item, index) => {
53+
// Render each paragraph in a <p> tag with a unique key
3454
return <p key={index}>{item}</p>;
3555
})}
3656
</article>

0 commit comments

Comments
 (0)