screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState } = React; function App() { const [itemsToShow, setItemsToShow] = useState(2); // Initially, display 5 items const [loading, setLoading] = useState(false); // Loading state // Demo text items const items = [ 'United States, Population: 331 million', 'Canada, Population: 38 million', 'Mexico, Population: 126 million', 'Brazil, Population: 214 million', 'Argentina, Population: 45 million', 'United Kingdom, Population: 67 million', 'France, Population: 67 million', 'Germany, Population: 83 million', 'Italy, Population: 60 million', 'Spain, Population: 47 million', 'Russia, Population: 145 million', 'Japan, Population: 126 million', 'South Korea, Population: 52 million', 'Australia, Population: 26 million', 'New Zealand, Population: 5 million', 'South Africa, Population: 59 million', 'Nigeria, Population: 206 million', 'Egypt, Population: 104 million', 'Kenya, Population: 54 million', 'Saudi Arabia, Population: 35 million', 'United Arab Emirates, Population: 10 million', 'Turkey, Population: 83 million', 'Greece, Population: 10 million', 'Canada, Population: 38 million', 'Mexico, Population: 126 million', 'Brazil, Population: 214 million', 'Argentina, Population: 45 million', 'Chile, Population: 19 million', 'Colombia, Population: 51 million', 'Peru, Population: 33 million', 'Australia, Population: 26 million', 'New Zealand, Population: 5 million', 'South Africa, Population: 59 million', 'Nigeria, Population: 206 million', 'Egypt, Population: 104 million', 'Kenya, Population: 54 million', 'Saudi Arabia, Population: 35 million', 'United Arab Emirates, Population: 10 million', 'Turkey, Population: 83 million', 'Greece, Population: 10 million', 'Canada, Population: 38 million', 'Mexico, Population: 126 million', 'Brazil, Population: 214 million', 'Argentina, Population: 45 million', 'Chile, Population: 19 million', 'Colombia, Population: 51 million', 'Peru, Population: 33 million', ]; // Function to load more items const loadMoreItems = () => { setLoading(true); // Set loading to true when loading starts setTimeout(() => { setItemsToShow(itemsToShow + 2); // Load 2 more items at a time setLoading(false); // Set loading to false when loading is complete }, 1000); // Simulate loading delay (you can adjust this) }; return ( <div className='container'> <h3 className='heading'>React Js Load More onclick Button</h3> <ul className='item-list'> {items.slice(0, itemsToShow).map((item, index) => ( <li key={index} className='list-item'>{item}</li> ))} </ul> {loading ? ( <p className='loading-text'>Loading...</p> // Display a loading message or spinner while loading ) : ( itemsToShow < items.length && ( <button onClick={loadMoreItems} className='load-more-button'>Load More</button> ) )} </div> ); } ReactDOM.render(<App />, document.getElementById("app")); </script> <style> * { box-sizing: border-box; } .container { margin: 0 auto; width: 600px; text-align: center; background: #fff; border-radius: 5px; padding: 20px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); } .heading { font-size: 24px; color: #333; } .item-list { list-style: none; padding: 0; } .list-item { font-size: 18px; margin: 10px 0; background-color: #f1f1f1; border-radius: 4px; padding: 10px; } .loading-text { font-size: 18px; color: #555; margin-top: 20px; } .load-more-button { background-color: #007bff; color: #fff; border: none; border-radius: 4px; padding: 10px 20px; font-size: 16px; cursor: pointer; margin-top: 20px; } .load-more-button:hover { background-color: #0056b3; } </style> </body> </html>