Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update App.js #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions 08-lorem-ipsum/final/src/App.js
Original file line number Diff line number Diff line change
@@ -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 (
<section className='section-center'>
<h3>tired of boring lorem ipsum?</h3>
{/* Form for selecting the number of paragraphs */}
<form className='lorem-form' onSubmit={handleSubmit}>
<label htmlFor='amount'>paragraphs:</label>
{/* Input for the number of paragraphs */}
<input
type='number'
name='amount'
id='amount'
value={count}
onChange={(e) => setCount(e.target.value)}
onChange={(e) => setCount(e.target.value)} // Update count state on input change
/>
<button className='btn'>generate</button>
</form>

{/* Display the generated paragraphs */}
<article className='lorem-text'>
{text.map((item, index) => {
// Render each paragraph in a <p> tag with a unique key
return <p key={index}>{item}</p>;
})}
</article>
Expand Down